Javascript Regex Word Boundary with optional non-word character

后端 未结 2 1422
灰色年华
灰色年华 2020-12-21 18:37

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

相关标签:
2条回答
  • 2020-12-21 18:50

    You need to account for 3 things here:

    • The main point is that a \b word boundary is a context-dependent construct, and if your input is not always alphanumeric-only, you need unambiguous word boundaries
    • You need to double escape special chars inside constructor RegExp notation
    • As you pass a variable to a regex, you need to make sure all special chars are properly escaped.

    Use

    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.
    0 讨论(0)
  • 2020-12-21 18:57

    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`);
    }
    
    0 讨论(0)
提交回复
热议问题