Fuzzy.js is a library for approximate (fuzzy) string matching. fuzzy.js' algorithm is very lightweight and is aimed to provide an experience which you may know from editors such as Sublime Text 2, TextMate and others.
Use the demo like an auto-completion search box on the web. Start typing to get a list of the 5 best matching available items. The demo applies fuzzy matching to the following items.
<script type='text/javascript' src='js/fuzzy.js'></script> <script type='text/javascript'>
var query = 'majs'; var term = 'main.js'; var match = fuzzy(term, query); console.log(match.score); // 8 console.log(match.query); // 'majs' console.log(match.term); // 'main.js' console.log(match.highlightedTerm); // '<em>m</em><em>a</em>in.<em>j</em><em>s</em>' // sorting an array of matches var matches = [ ... ]; matches.sort(fuzzy.matchComparator); // sorts descending
</script>
<script type='text/javascript' src='js/fuzzy.js'></script> <script type='text/javascript'>
// change the character sequences that are used to highlight matches fuzzy.highlighting.before = '<'; fuzzy.highlighting.after = '>'; // enable sub term matches. See below for an example fuzzy.analyzeSubTerms = true; var query = 'Halleluja', var term = 'luja'; var match = fuzzy(term, query); console.log(match.score); // 10 console.log(match.query); // 'luja' console.log(match.term); // 'Halleluja' console.log(match.highlightedTerm); // 'Halle<l><u><j><a>' // now let us check this out without sub term matches fuzzy.analyzeSubTerms = false; match = fuzzy(term, query); console.log(match.score); // 8 console.log(match.query); // 'luja' console.log(match.term); // 'Halleluja' console.log(match.highlightedTerm); // 'Ha<l>lel<u><j><a>'
</script>