albertein albertein - 1 year ago 133
Javascript Question

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"

Answer Source

select2 allows you to implement your own "matcher" functions (as seen on their docs), using that and some regexp you can do something like:

    matcher: function(term, text, opt) {
        //We call to uppercase to do a case insensitive match
        //We replace every group of whitespace characters with a .+
        //matching any number of characters
        return text.toUpperCase().match(term.toUpperCase().replace(/\s+/g, '.+'));

A matcher function is invoked against every select2 list element when filtering / searching the list, you could implement any kind of custom search using that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download