Negating a backreference in Regular Expressions

Instead of a negated character class, you have to use a negative lookahead:

\bvalue\s*=\s*(["'])(?:(?!\1).)*\1

(?:(?!\1).)* consumes one character at a time, after the lookahead has confirmed that the character is not whatever was matched by the capturing group, (["'']). A character class, negated or not, can only match one character at a time. As far as the regex engine knows, \1 could represent any number of characters, and there’s no way to convince it that \1 will only contain " or ' in this case. So you have to go with the more general (and less readable) solution.

Leave a Comment