Using this regexp I can get a match on मंत्री but exclude मुख्यमंत्री:
var regex = XRegExp("(?:^|\\P{L})मंत्री(?=\\P{L}|$)");
What this does is match मंत्री if it:
Is at the beginning of the string or preceded by a character which Unicode considers a non-Letter, and
Is at the end of the string or followed by a character which Unicode considers a non-Letter.
Note that this is slightly different from what \b
does because \b
does not match digits. For instance, /\bmantri\b/
won't match mantri123
because 1
, 2
, and 3
are considered to be part of words and thus do not mark a word boundary. If you want something that emulates \b
then this would do it:
var regex = XRegExp("(?:^|[^\\p{L}\\p{N}])मंत्री(?=[^\\p{L}\\p{N}]|$)");
The difference with the first regexp is that with this one मंत्री cannot be preceded or followed by a digit.
I've used a negative lookahead at the end of the regular expression so the character that follows your word is excluded from the results. There is no equivalent negative lookbehind so if there is a character before मंत्री, it will appear in the results. You'll have to decide what you want to do with this character for your specific application.