重音不敏感的正则表达式

Reg*_*xme 8 regex unicode jquery highlight diacritics

我的代码:

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>';
  }));
 }

});
Run Code Online (Sandbox Code Playgroud)

我想突出显示带重音的字母,即:

$('body').highlight("cao");
Run Code Online (Sandbox Code Playgroud)

应突出显示:[ção] OR [çÃo] OR [cáo] OR expre [cão] tion或[Cáo] tion

我怎样才能做到这一点?

tch*_*ist 5

执行此操作的唯一正确方法是首先通过Unicode规范化表D,规范分解运行它.

然后剥离我们产生的任何标记(\pM字符,或者可能\p{Diacritic},取决于),并根据de/un-marked版本运行匹配.

在任何情况下都不要硬编码一堆文字.伊克!

Boa sorte!

  • 你愿意分享一些代码示例!?! (3认同)