In JavaScript:
"ab abc cab ab ab".replace(/\bab\b/g, "AB");
correctly gives me:
"AB abc cab AB AB"
When I use utf-8 characters though:
"αβ αβγ γαβ αβ αβ".replace(/\bαβ\b/g, "AB");
the word boundary operator doesn’t seem to work:
"αβ αβγ γαβ αβ αβ"
Is there a solution to this?
The word boundary assertion does only match if a word character is not preceded or followed by another word character (so
.\b.is equal to\W\wand\w\W). And\wis defined as[A-Za-z0-9_]. So\wdoesn’t match greek characters. And thus you cannot use\bfor this case.What you could do instead is to use this: