-
-
Save technomorph/1c4223ccd777574f937b747e955d1d38 to your computer and use it in GitHub Desktop.
Fuzzy Regex match
This file contains hidden or 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
// This will allow unordered search terms to match relevant string | |
// e.g. Really Long String will be matched by | |
// - long string | |
// - long really | |
// - all ring | |
// ..etc | |
// I have used this anonymous function to override the matcher function in select2 | |
function(term, text) { | |
// Build Regex String | |
var matchTerm = '.*'; | |
// Split all the search terms | |
var terms = term.split(" "); | |
for(var i = 0; i < terms.length; i++) { | |
matchTerm += '(?=.*' + terms[i] + '.*)'; | |
}; | |
matchTerm += '.*'; | |
// Convert to Regex | |
// => /.*(?=.*TERM1.*)(?=.*TERM2.*).*/ | |
var matchRegex = new RegExp(matchTerm.toUpperCase()); | |
return text.toUpperCase().match(matchRegex); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment