1

I am trying to return boolean value for matching input string with a exact regex pattern but given code is not working as expected for string containing parentheses.Below is sample example of the same.

function myFunction() {
    var str = "Account Opening Member Onboarding Assignment Cube (AOMOA)";
    var patt = new RegExp("/\bAccount Opening Member Onboarding Assignment Cube (AOMOA)\b/");
    return patt.test(str);
}
rahul shukla
  • 233
  • 3
  • 11
  • escape the parantheses `\(` and `\)`. Otherwise they will be interpreted as capture group – abhishekkannojia Mar 20 '17 at 14:25
  • @abhishekkannojia how do I do that at run time when any string is passed to function? rather more suitable question is how do I handle any special characters without affecting the matching process?? – rahul shukla Mar 20 '17 at 14:27
  • @rahulshukla Escape the strings before passing them in RegExp. – abhishekkannojia Mar 20 '17 at 14:30
  • Just escaping a string won't solve the problem as it requires 1) whitespace word boundaries rather than `\b`, 2) syntax fix for the constructor notation, 3) only then escaping the literal pattern part. Thus, [Escape regexp strings?](https://stackoverflow.com/questions/6828637/escape-regexp-strings) cannot be used as a duplicate reason (the accepted answer there is wrong, and it is by itself a dupe of [Is there a RegExp.escape function in Javascript?](https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript)). – Wiktor Stribiżew Sep 12 '18 at 06:48

2 Answers2

1

It is most probable that you look for whole words that are in between whitespaces, as the search phrases you are passing to the regex may start/end with special chars. In this case, use (?:^|\s) instead of the first \b and (?!\S) instead of the last \b.

Another point is that you need to escape the search phrase for the eventual special chars to be treated as literal chars.

Use

function myFunction(key) {
    var str = "Text with Account Opening Member Onboarding Assignment Cube (AOMOA) string inside";
    var patt = new RegExp("(?:^|\\s)" + key.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "(?!\\S)");
    return patt.test(str);
}

var key="Account Opening Member Onboarding Assignment Cube (AOMOA)";
console.log(myFunction(key));
Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • it would be helpful if you can explain this regular expression "(?:^|\\s)" ? @wiktor-stribiżew – rahul shukla Mar 22 '17 at 06:31
  • The `(?:^|\s)` subpattern is a non-capturing group (`(?:....)`, used only to group subpatterns rather than store the submatches as additional parts of the resulting array) that contains two alternative branches: 1) `^` that matches the start of the string, or (`|`) 2) `\s` that matches a whitespace. So the *key* will only get matched if it is preceded by a whitespace or start of string position. The `(?!\S)` is a negative lookahead that fails the match if there is no non-whitespace symbol after the *key* (so, there must be a whitespace or end of string). – Wiktor Stribiżew Mar 22 '17 at 07:32
0

function myFunction(key) {
    var str = "Text with Account Opening Member Onboarding Assignment Cube (AOMOA) string inside";
    var patt = new RegExp("(?:^|\\s)" + key.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "(?!\\S)");
    return patt.test(str);
}

var key="Account Opening Member Onboarding Assignment Cube (AOMOA)";
console.log(myFunction(key));
rahul shukla
  • 233
  • 3
  • 11