Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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!)".

about 4 years ago · Juan Pablo Isaza Report

0

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):
(?:^|[^\\]|\\\\)(")

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!