Skip to content

Instantly share code, notes, and snippets.

@cauldyclark15
Forked from samknight/fuzzyregex.js
Created August 4, 2017 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cauldyclark15/dae8f8e6b7d36587c3b3f9aae2d1009f to your computer and use it in GitHub Desktop.
Save cauldyclark15/dae8f8e6b7d36587c3b3f9aae2d1009f to your computer and use it in GitHub Desktop.
Fuzzy Regex match
// 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