You are facing this issue because your regular expression is not correct.
That's because the \b
word boundary works well with Latin
string but struggles with RTL
and Unicode
character.
Try to replace it with g
for global and u
for Unicode chars
const regex = new RegExp(`(${highlightWordsArray.join("|")})`, "gu")
You can check this article if you wanna discover more