简体   繁体   中英

Javascript RegExp for exact multiple words with special characters match

I'm using RegExp for multiple words match. It has dynamic values so when a special character like "(" comes it takes that as an expression and shows Uncaught SyntaxError: Invalid regular expression error.

 let text = 'working text and (not working text' let findTerm = ['working text', '(not working text'] let replaceFromRegExp = new RegExp('\\b'+`(${findTerm.join("|")})`+'\\b', 'g') text = text.replace(replaceFromRegExp, match => "<mark>" + match + "</mark>") console.log(text)

You need universal word boundaries that will require a non-word char or start of string before a search word, and a non-word char or end of string after a search string.

Note you need to also sort the findTerm items in the descending order by length to avoid overlapping term issues.

Finally, do not forget to escape the findTerm items to be used in a regex pattern.

You can use

 let text = 'working text and (not working text' let findTerm = ['working text', '(not working text'] findTerm.sort((a, b) => b.length - a.length); let replaceFromRegExp = new RegExp(String.raw`(?:\\B(?!\\w)|\\b(?=\\w))(?:${findTerm.map(x => x.replace(/[-\\/\\\\^$*+?.()|[\\]{}]/g, '\\\\$&')).join("|")})(?:(?<=\\w)\\b|(?<!\\w)\\B)`, 'g') console.log(replaceFromRegExp) text = text.replace(replaceFromRegExp, "<mark>$&</mark>") console.log(text)

Note that "<mark>$&</mark>" is a shorter way of saying match => "<mark>" + match + "</mark>" , as $& is a backreference to the whole match value in a string replacement pattern.

The regex is

/(?:\B(?!\w)|\b(?=\w))(?:\(not working text|working text)(?:(?<=\w)\b|(?<!\w)\B)/g

See the regex demo . Details :

  • (?:\\B(?!\\w)|\\b(?=\\w)) - either a non-word boundary if the next char is not a word char, or a word boundary if the next char is a word char
  • (?:\\(not working text|working text) - a non-capturing group matching one of the alternative patterns set in the findTerm array
  • (?:(?<=\\w)\\b|(?<!\\w)\\B) - either a word boundary if the preceding char is a word char, or a non-word boundary if the preceding char is not a word char.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM