我有一个自动填充功能,它有多个类别,其值与用户输入中的任何字母集匹配。每个类别值都有多个字符串存储在其中(请参阅下面的value.application示例数据)。
这很好用,但我希望它只匹配字母组合的整个单词intead。如何实现这一目标?
示例:
源数据:"所有来电最高的"
用户进入:" 所有"
返回:" 全部 致电呃高 est"
寻找它只返回:" 所有"
source: function(request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); // match letters in string
response($.grep(SearchList, function(value) {
return matcher.test(value.label) ||
matcher.test(value.value) ||
matcher.test(value.sku) ||
matcher.test(value.application) ||
matcher.test(value.discontinuedproductlist) ||
matcher.test(value.type);
}));
},

如上所述,这些值中的每一个都有多个单词串。
(值。应用)数据示例:"干湿冷热烧焦烧焦脏污脏污"
答案 0 :(得分:0)
能够通过添加" \ b"来完成这项工作。正则表达式属性到搜索词的每一边(参见matcherWords)。
现在我可以为某些类别返回字母组合匹配,并且只有整个单词匹配其他类别。希望这有助于其他人。
source: function(request, response) {
var matcherLetters = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); // match letters in string
var matcherWords = new RegExp("\\b" + $.ui.autocomplete.escapeRegex(request.term) + "\\b", "i"); // match words in string
response($.grep(SearchList, function(value) {
return matcherLetters.test(value.label) ||
matcherLetters.test(value.value) ||
matcherWords.test(value.sku) ||
matcherWords.test(value.application) ||
matcherWords.test(value.discontinuedproductlist) ||
matcherWords.test(value.type);
}));
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;