最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 autoplete plugin.

source: function(request, response) {
        var matcher = new RegExp($.ui.autoplete.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.autoplete.escapeRegex(request.term) +
                     ")(?![^<>]*>)(?![^&;]+;)", "gi"
                 ), "<strong>$1</strong>"),
             value: text,
             option: this
            };
      }));

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 autoplete plugin.

source: function(request, response) {
        var matcher = new RegExp($.ui.autoplete.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.autoplete.escapeRegex(request.term) +
                     ")(?![^<>]*>)(?![^&;]+;)", "gi"
                 ), "<strong>$1</strong>"),
             value: text,
             option: this
            };
      }));
Share Improve this question edited Dec 4, 2013 at 12:39 falsarella 12.4k10 gold badges74 silver badges118 bronze badges asked Dec 15, 2011 at 12:44 SaurabhSaurabh 1571 gold badge3 silver badges12 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10

In Javascript RegExp, ^ means startsWith:

var matcher = new RegExp("^" + $.ui.autoplete.escapeRegex(request.term), "i");

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论