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

123
Visualizações
match the last element with javscript regex without starting with greedy

I'm trying to resolve the following scenario with javascript regexp:

I have the following text:

<foo>a</foo>blah <bar>blah</bar><abc>dsdfsdf</abc> blah<foo>b</foo><blah></blah>{COMPANY_NAME}

I would like to catch the {COMPANY_NAME} with text inside the previous foo tag.

so here i'm trying to find the text b that exists inside the first foo tag behind the COMPANY_NAME.

the only way i found to do that is to start with a greedy character, which means to use this:

/.*<foo>(.*?)<\/foo>.*?{COMPANY_NAME}/

if I don't start with the greedy selection at the begginging (.*) then it will provide the first foo instead of the last one, the problem is that this document is really big and it takes a lot of memory and it takes a long time to return each match and i'mm doing a lot of matches.

is there a way to resolve it without starting with the greedy .*? and just to return the last foo properly so the match will only contain what i need and not more then that ?

thanks

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

0

You may use this regex without initial greedy .*:

<foo>((?:(?!<\/?foo>).)*?)<\/foo>(?:(?!<\/?foo>).)*?{COMPANY_NAME}

RegEx Demo

Here (?:(?!<\/?foo>).)*? is tempered greedy pattern that matches zero or more characters where each character must not be followed by <foo> or </foo>.

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