1

What is the correct way to check for a whole word in a unicode string in Javascript.

This works for ASCII only:

var strHasWord = function(word, str){
   return str.match(new RegExp("\\b" + word + "\\b")) != null;
};

I tried XRegExp as follows but this does not work either.

var strHasWord = function(word, str){
  // look for separators/punctuation characters or if the word is the first or the last in the string
  var re = XRegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)");
  return re.test(str);
  //return str.match(re);
}

Any suggestions. Thanks.

EDIT 1 The following seems to do the trick.

var function strHasWord = function(word, str){
 var re = RegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)", "u");
   return str.match(re) != null;
    //return re.test(str);
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user4848830
  • 779
  • 12
  • 22
  • Does it _have_ to be regex? Because otherwise, a good old [`indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) might do the trick. – domsson Jan 18 '20 at 06:41
  • @domsson : Can you please elaborate, how can I use indexOf() ? I need to check for whole word not just substring ? – user4848830 Jan 18 '20 at 06:55
  • another way is native search in string , console this : `'hi, my name is neo'.search('neo') > -1` – Neo Anderson Jan 18 '20 at 06:56
  • Mr. Anderson (@NeoAnderson) : it will match "'hi, my name is theneo'.search('neo')". I need only whole words. Thanks. – user4848830 Jan 18 '20 at 07:08
  • @user4848830 so what about this one : `\b(neo)\b` and check here for test : https://regex101.com/r/m9zSUO/2 – Neo Anderson Jan 18 '20 at 07:56
  • 1
    @NeoAnderson: kindly check my question. this was the first thing i tried and it does not work for unicode. I updated your test link: https://regex101.com/r/m9zSUO/3 – user4848830 Jan 18 '20 at 08:28

0 Answers0