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

111
Visualizações
How to match a pattern but not when a specific tag is present

I'm trying to match: --This is a text and it <b>can contain other </b> tags--

I will wrap matched pattern with <u></u> But i'm trying to avoid <br> tag only. I.e it shouldn't match if the string has <br> in between --

text.replace(/(?:--)(?:(?!--|\s))((?!<br>).*?)(?:--)/g,'<u>$1</u>')  

Unfortunately, my regex matches

--This is a text, <b>can contain other</b>tags but don't match if <br> is present--

I don't expect it to match since <br> is present.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Make (?!<br>). as a group, then quantify this group, instead of quantifying . only. Aslo, ? is redundant after * because ? means 0 or 1 while * means 0 or more, which already cover 1.

(?:--)(?:(?!--|\s))((?:(?!<br>).)*)(?:--)

Bonus: You don't need capture group if you use positive look ahead and positive look behind for --, if your environment support it. This match itself will exclude the --.

(?<=--)(?:(?!--|\s))(?:(?!<br>).)*(?=--)
about 4 years ago · Juan Pablo Isaza Relatório

0

First check if string does not contain <br> and then continue regex:

if (!/--.*?<br>.*?--/.test(text)) {
    text.replace(/(?:--)(?:(?!--|\s))((?!<br>).*?)(?:--)/g,'<u>$1</u>')  
}

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