0

This regex will match the exact or whole word 'fire' :

/\bfire\b/i

and in the google script i use this syntax to convert string into regex , where the keywords is the variable contains the word to search :

RegExp("\\b"+keywords+"\\b","i")

it works as i expected. But now i want to do the inverse. I want to NOT MATCH.

The regex bellow i tested in https://www.regextester.com/ and works fine.

/^((?!\bfire\b).)*$/i

It will returns TRUE if it DOES NOT contains word 'fire'. But i'm having problem to convert it into regex in google script. I'm just escaping all the special characters using '\' :

RegExp("\^\(\(\?\!\\b"+keywords+"\\b\)\.\)\*\$","i")

it returns wrong output. Can someone help ?

andio
  • 1,574
  • 9
  • 26
  • 45

1 Answers1

0

I'd recommend using a template literal so you need a lot less back slashes:

const r = new RegExp(`^((?!\\b${keyword}\\b).)*$`, 'i')

Also, you may want to escape the keywords to support special characters. If you are interested, you can see this answer to see how to do it.

Martí
  • 2,651
  • 1
  • 4
  • 11