Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To: Modify jquery.autocomplete regex to show results that starts with the given input

I dont know enough to modify following code that will show me the word starting with the inputted characters.For eg.: If I type E or e it shows me "Electrical,Electronics,Mechanical" but I want only "Electrical,Electronics" to be displayed.How to do this?

I am using jquery autocomplete plugin.

source: function(request, response) {
        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        response(select.children("option").map(function() {

         var text = $(this).text();
         if (this.value && (!request.term || matcher.test(text)))
             return {
             label: text.replace(
                 new RegExp(
                 "(?![^&;]+;)(?!<[^<>]*)(" +
                     $.ui.autocomplete.escapeRegex(request.term) +
                     ")(?![^<>]*>)(?![^&;]+;)", "gi"
                 ), "<strong>$1</strong>"),
             value: text,
             option: this
            };
      }));
like image 656
Saurabh Avatar asked Dec 15 '11 12:12

Saurabh


1 Answers

In Javascript RegExp, ^ means startsWith:

var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i");
like image 183
falsarella Avatar answered Oct 02 '22 16:10

falsarella