gpt4 book ai didi

匹配字符串的重音和非重音版本的 Javascript 正则表达式

转载 作者:行者123 更新时间:2023-11-29 19:04:48 27 4
gpt4 key购买 nike

我正在寻找一种使用正则表达式的方法,该正则表达式将使用 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>

所以现在,如果我写(例如)Montréal,它会工作并在我的菜单中找到 Montréal,但如果我写 Montreal,RegEx 将找不到它。

我在网上看过,但找不到合适的正则表达式。

最佳答案

这并不简单 - 基本上唯一的方法是在将重音字符传递到您的正则表达式之前用非重音版本替换重音字符。

这篇文章有一些示例代码(寻找“嘿伙计,我来这里只是为了复制/粘贴”部分),还有很多注意事项:Accent Folding for Auto-Complete

关于匹配字符串的重音和非重音版本的 Javascript 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43766356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com