Created
March 6, 2015 07:56
-
-
Save andik/f51056ad6d43a6614c65 to your computer and use it in GitHub Desktop.
Fuzzy matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
from http://willmcgugan.com/blog/tech/2015/3/5/sublime-text-like-fuzzy-matching-in-javascript/