Question

I need a regular expression to check if a character doesn't appear in double quotes("). For example, if i have something like <b>"ABC<DEF"</b>, then the regular expression should match < of both the tags and it should not match < between the double quotes.

Was it helpful?

Solution

I guess you meant "should NOT match < between the double quotes". In this case, a lookahead does the trick:

s = '<b>"ABC<DEF" match<this "ignore<this" end'
s.replace(/<(?=("[^"]*"|[^"]+)+$)/g, "@")

// "@b>"ABC<DEF" match@this "ignore<this" end"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top