Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement sublime text like fuzzy search?

How can i implement a sublime-like fuzzy search on select2?

Example, typing "sta jav sub" would match "Stackoverflow javascript sublime like"

like image 394
albertein Avatar asked Jun 04 '13 00:06

albertein


People also ask

What is fuzzy search example?

For example, if a user types "Misissippi" into Yahoo or Google -- both of which use fuzzy matching -- a list of hits is returned along with the question, "Did you mean Mississippi?" Alternative spellings and words that sound the same but are spelled differently are given.

What is fuzzy search how fuzzy search works?

A fuzzy search searches for text that matches a term closely instead of exactly. Fuzzy searches help you find relevant results even when the search terms are misspelled. To perform a fuzzy search, append a tilde (~) at the end of the search term.

What is fuzzy search JavaScript?

6 min · Sabesh Bharathi. Fuzzy Search refers to the process of approximately searching for a given search query. It may also be called as a “typo tolerant search”.


1 Answers

Here's an alternate matching function. http://jsfiddle.net/trevordixon/pXzj3/4/

function match(search, text) {     search = search.toUpperCase();     text = text.toUpperCase();      var j = -1; // remembers position of last found character      // consider each search character one at a time     for (var i = 0; i < search.length; i++) {         var l = search[i];         if (l == ' ') continue;     // ignore spaces          j = text.indexOf(l, j+1);     // search for character & update position         if (j == -1) return false;  // if it's not found, exclude this item     }     return true; } 

This one's faster (according to this test in Chrome), which may start to matter if you're filtering a lot of items.

like image 199
Trevor Dixon Avatar answered Sep 24 '22 07:09

Trevor Dixon