Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda