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

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

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 Report

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