我正在寻找一种使用正则表达式匹配使用Javascript的重音和非重音版本的字符串的方式。Javascript正则表达式匹配重音和非重音版本的字符串
我延长jQuery的UI的自动完成功能在自动完成周围添加粗体字的HTML标签,这里是我的代码:
<script>
// Add feature to autocomplete to make the searched query in bold letter
$.extend($.ui.autocomplete.prototype, {
_renderItem: function (ul, item) {
var searchMask = this.element.val();
var replaceMask = "<b style=\"\">$&</b>";
var splittedMask = searchMask.split(' ');
var html = regEx = '';
// Fix I added so it works with multiple words.
splittedMask.forEach(function(word){
if (word.length >= 2){
// Here is the part that should interest you
regEx = new RegExp(word, "ig");
if (html == ''){
html = item.label.replace(regEx, replaceMask);
} else {
html = html.replace(regEx, replaceMask);
}
} else {
html = item.label;
}
});
return $("<li></li>")
.data("item.autocomplete", item)
.append($("<span></span>").html(html))
.appendTo(ul);
}
});
</script>
所以现在,如果我写的(例如)蒙特利尔,它的工作原理和在我的菜单中找到蒙特利尔,但如果我写蒙特利尔,它不会被RegEx
发现。
我在网上找过,但找不到正确的RegEx。
您将需要首先对输入进行归一化处理,因为没有办法将'é'与'e'匹配。尽管如此,你可以考虑用'[eé]'替换所有'e'。 –
@WiktorStribiżew它实际上是一个好主意的人,我用e去[eé]和a去做[aà]。你应该做一个答案,我相信这是一个很好的解决我的问题 –
我添加了这个建议的答案。 –