重音不敏感的正则表达式 [英] accent insensitive regex

查看:122
本文介绍了重音不敏感的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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");

应突出显示:[ção]或[çÃo]或[cáo] OR表达式或[cáo] tion

should highlight: [ção] OR [çÃo] OR [cáo] OR expre[cão]tion OR [Cáo]tion

我该怎么做?

推荐答案

唯一的正确方法是首先通过 Unicode规范化形式D (规范分解)运行它.

The sole correct way to do this is to first run it through Unicode Normalization Form D, canonical decomposition.

然后您剥去我们产生的所有标记(\pM个字符,或者也许取决于\p{Diacritic}),然后将您的匹配项与未标记/未标记的版本进行匹配.

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!

蟒蛇突击!

这篇关于重音不敏感的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆