Domanda

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.

È stato utile?

Soluzione

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"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top