The string will replace the exact match in Cyrillic

I want to use regex to replace a string with cyrillic. I want to use the exact match option. My replace string works with latin characters and looks like this:

'Edin'.replace(/\Edin\b/gi, ''); // Output is "" 

The same expression does not work with Cyrillic characters

 ''.replace(/\\b/gi, ''); // Output is still '' 
+6
source share
2 answers

The problem is \b reducing word boundaries that matches the position on the word boundary. The word boundary is defined as (^\w|\w$|\W\w|\w\W) . And in turn, the character \w is a set of ASCII characters [A-Za-z0-9_]. Obviously, Cyrillic characters do not fall into this set.

For example, for the same reason /\w+/ regular expression will not match the Cyrillyc line.

+7
source

Like dfsq , the problem is related to the word. If you delete \b , you will get the desired result, but this is a completely different regular expression. He will replace One also in those cases when he is part of the word. To avoid this, you can use a negative lookahead and determine which letters should not appear behind, because they can be part of the word.

 ''.replace(/\(?![A-])/gi, ''); 
+4
source

Source: https://habr.com/ru/post/975870/


All Articles