Search code examples
javascriptregexprototypejs

Regex detect if key is not inside quote


I am totally new to regex and I just can't get it to work. I have this Javascript code

html.replace(new RegExp(util.escapeRegex(key), 'g'), 'something');

But I would only like it to fire if util.escapeRegex(key) is outside from quotes "/'

Note that key is something like :key:


Solution

  • Assuming no double quote is unbalanced, this should do the trick for you.

    html.replace(new RegExp(util.escapeRegex(key) + '(?=(?:(?:[^"]*"){2})*[^"]*$)', 'g'), 'something');