I am looking to find a keyword match in a string. I am trying to use word boundary, but this may not be the best case for that solution. The keyword could be any word, and c
You need to account for 3 things here:
\b
word boundary is a context-dependent construct, and if your input is not always alphanumeric-only, you need unambiguous word boundariesUse
let userStr = 'why hello there, or should I say #hello there?';
let keyword = '#hello';
let re_pattern = `(?:^|\\W)(${keyword.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})(?!\\w)`;
let res = [], m;
// To find a single (first) match
console.log((m=new RegExp(re_pattern).exec(userStr)) ? m[1] : "");
// To find multiple matches:
let rx = new RegExp(re_pattern, "g");
while (m=rx.exec(userStr)) {
res.push(m[1]);
}
console.log(res);
Pattern description
(?:^|\\W)
- a non-capturing string matching the start of string or any non-word char(${keyword.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})
- Group 1: a keyword value with escaped special chars(?!\\w)
- a negative lookahead that fails the match if there is a word char immediately to the right of the current location.Check whether the keyword already begins with a special character. If it does, don't include it in the regular expression.
var re;
if ("#@".indexOf(keyword[0]) == -1) {
re = new RegExp(`[@#]?\b${keyword}\b`);
} else {
re = new RegExp(`\b${keyword}\b`);
}