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

121
Views
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 answers
Answer question

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 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!