正则表达式,将所有非字母字符替换为下划线,保持第一个字母不变。

huangapple go评论50阅读模式
英文:

Regular Expression for all non letter changes to underscore remain fixed first letter # intact

问题

text.replace(/(?!^)[^\p{L}\p{N}।]+/gu, '_');
英文:

I use this regex for changing all symbols or any non digit character to underscore remaining first # value as it is.

text.replace(/(?!^)[^\p{L}\p{N}]+/gu, '_');

This will working fine but for bengali letter when user type চার it prints like that: চ__া_র

Please provide me the correct regex.

答案1

得分: 2

The second char (\u09BE) is a 09BE BENGALI VOWEL SIGN AA that belongs to a "Mark, spacing combining" Unicode category (Mc).

That means, you need to add a diacritic mark Unicode category class to the negated character class:

/(?!^)[^\p{L}\p{N}\p{M}]+/gu

See the JavaScript demo:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log("চার".replace(/(?!^)[^\p{L}\p{N}\p{M}]+/gu, '_'))

<!-- end snippet -->

英文:

The second char (\u09BE) is a 09BE BENGALI VOWEL SIGN AA that belongs to a "Mark, spacing combining" Unicode category (Mc).

That means, you need to add a diacritic mark Unicode category class to the negated character class:

/(?!^)[^\p{L}\p{N}\p{M}]+/gu

See the JavaScript demo:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(&quot;চার&quot;.replace(/(?!^)[^\p{L}\p{N}\p{M}]+/gu, &#39;_&#39;))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 17:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定