'How to replace my current regular expression without using negative lookbehind

I have the following regular expression which matches on all double quotes besides those that are escaped:

i.e: enter image description here

The regular expression is as follows:

((?<![\\])")

How could I alter this to no longer use the negative lookbehind as it is not supported on some browsers?

Any help is greatly appreciated, thanks!

I wasn't able to get anything currently working



Solution 1:[1]

You can match

/\\"|(")/

and keep only captured matches. Being so simple, it should work with most every regex engine.

Demo

This matches what you don't want (\\")--to be discarded--and captures what you do want (")--to be kept.

This technique has been referred to by one regex expert as The Greatest Regex Trick Ever. To get to the punch line at the link search for "(at last!)".

Solution 2:[2]

Neither of these may be a completely satisfactory solution.

This regex won't just match unescaped ", there's additional logic required to check if the 1st character of captured groups is " and adjust the match position.:

(?:^|[^\\])(")

version 1

This may be a better choice, but it depends on positive lookahead - which may have the same issue as negative lookbehind.

Version 1a (again requires additional logic) (?:^|\b)(?=[^\\])(")

Version 2a (depends on positive lookahead)

(?:^|\b|\\\\)(?=[^\\])(")

version 2

Assuming you need to also handle escaped slashes followed by escaped quotes (not in the question, but ok):

Version 1a (requires the additional logic):
(?:^|[^\\]|\\\\)(")

Solution 3:[3]

Building on this answer, I'd like to add that you may also want to ignore escaped backslashes, and match the closing quote in this string:

"ab\\"

In that case, /\\[\\"]|(")/g is what you're after.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Pygy