不区分重音的正则表达式
我的代码:
jQuery.fn.extend({
highlight: function(search){
var regex = new RegExp('(<[^>]*>)|('+ search.replace(/[.+]i/,"$0") +')','ig');
return this.html(this.html().replace(regex, function(a, b, c){
return (a.charAt(0) == '<') ? a : '<strong class="highlight">' + c + '</strong>';
}));
}
});
我想突出显示带有重音符号的字母, 即:
$('body').highlight("cao");
应该突出显示:[ção] OR [ção] OR [cáo] OR expre[cão]tion OR [Cáo]tion
我该怎么做?
My code:
jQuery.fn.extend({
highlight: function(search){
var regex = new RegExp('(<[^>]*>)|('+ search.replace(/[.+]i/,"$0") +')','ig');
return this.html(this.html().replace(regex, function(a, b, c){
return (a.charAt(0) == '<') ? a : '<strong class="highlight">' + c + '</strong>';
}));
}
});
I want to highlight letters with accents,
ie:
$('body').highlight("cao");
should highlight: [ção] OR [çÃo] OR [cáo] OR expre[cão]tion OR [Cáo]tion
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行此操作的唯一正确方法是首先通过 Unicode 规范化形式 D(规范分解)运行它。
然后,您剥离我们产生的任何标记(
\pM
字符,或者可能\p{Diacritic}
,具体取决于),并针对取消/未标记的版本运行您的匹配。在任何情况下都不要对一堆文字进行硬编码。哎呀!
蟒蛇排序!
The sole correct way to do this is to first run it through Unicode Normalization Form D, canonical decomposition.
You then strip our any Marks that result (
\pM
characters, or perhaps\p{Diacritic}
, depending), and run your match against the de/un-marked version.Do not under any circumstances hardcode a bunch of literals. Eek!
Boa sorte!
您需要提出一个替代字符表,并基于该表动态生成正则表达式。例如:
对于
search = 'cao'
,这将生成一个模式[cCç][aAãñá]o
。You need to come up with a table of alternative characters and dynamically generate a regex based on that. For example:
Then for
search = 'cao'
this will generate a pattern[cCç][aAãÃá]o
.