Skip to content

Instantly share code, notes, and snippets.

@andik
Created March 6, 2015 07:56
Show Gist options
  • Save andik/f51056ad6d43a6614c65 to your computer and use it in GitHub Desktop.
Save andik/f51056ad6d43a6614c65 to your computer and use it in GitHub Desktop.
Fuzzy matching
function fuzzy_match(text, search)
{
/*
Parameter text is a title, search is the user's search
*/
// remove spaces, lower case the search so the search
// is case insensitive
var search = search.replace(/\ /g, '').toLowerCase();
var tokens = [];
var search_position = 0;
// Go through each character in the text
for (var n=0; n<text.length; n++)
{
var text_char = text[n];
// if we watch a character in the search, highlight it
if(search_position < search.length &&
text_char.toLowerCase() == search[search_position])
{
text_char = '<b>' + text_char + '</b>';
search_position += 1;
}
tokens.push(text_char);
}
// If are characters remaining in the search text,
// return an empty string to indicate no match
if (search_position != search.length)
{
return '';
}
return tokens.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment