用常规字符代替变音符时做例外

发布于 2025-01-30 10:47:27 字数 216 浏览 3 评论 0 原文

试图对已转换为常规字母的所有角色进行例外。

txtvalue.normalize('nfd')。替换(/[\ u0300- \ u036f]/g,'');

您可以看到,我替换了普通字母的所有特殊字符,但是现在我想添加一个“ char”“ç”的例外,它的资本等效“ç”分别为 u00e7 u00c7

Trying to make an exception to all characters that have been converted to regular letters.

txtValue.normalize('NFD').replace(/[\u0300-\u036f]/g, '');

As you can see, I replace all the special characters for normal letters, but now I would like to add an exception for the char "ç" and it's capital equivalent "Ç", which are u00e7 and u00c7 respectively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋心╮凉 2025-02-06 10:47:27

我看到的唯一简单方法不是优化,而是正确地完成工作:

const text = "Çééé éÇé àç" // test this string
  .replace(/\u00e7/g, '__minC__') // save wanted chars position
  .replace(/\u00c7/g, '__majC__')
  .normalize('NFD') // normalize to prepare diacritic edit
  .replace(/\p{Diacritic}/gu, '') // replace all diacritics
  .replace(/__minC__/g, 'ç') // restore wanted chars
  .replace(/__majC__/g, 'Ç')
  
  console.log(text)

在这种情况下,我们从 unicode属性逃逸

The only simple way I see is not optimized but do the job properly :

const text = "Çééé éÇé àç" // test this string
  .replace(/\u00e7/g, '__minC__') // save wanted chars position
  .replace(/\u00c7/g, '__majC__')
  .normalize('NFD') // normalize to prepare diacritic edit
  .replace(/\p{Diacritic}/gu, '') // replace all diacritics
  .replace(/__minC__/g, 'ç') // restore wanted chars
  .replace(/__majC__/g, 'Ç')
  
  console.log(text)

In this case we use \p{Diacritic} from Unicode Property Escapes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文