当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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

};

}));

网友答案:

In Javascript RegExp, ^ means startsWith:

var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i");
分享给朋友:
您可能感兴趣的文章:
随机阅读: