Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda