Delete multiple tag instances with a condition

As I said in the post, I'm just a regex padawan.

It all started in this thread: stack overflow

The question was to find: <br />tags inside<pre></pre>

I made this template: .*?<pre>.*\s?((<br\s*?\/?>\s?).*?){1,}<\/pre>

I answered the question.

Everything was in order, as the question arose:

SUB-Question: How to find ANYWHERE <br />inside a tag <pre>?

I translated this into: How to find multiple instances of a word with a condition

Explanation of my regex:

  • .*? - Unwanted quantifier: any number of characters
  • <pre> - Following the tag
  • .*\s? - any number of any char + possible whitespace
  • (<br\s*?\/?>\s?) - Follow the tag we are looking for + possible whitespace
  • ((<br \/>\s?).*?){1,} ( <br /> + char + char) 1 . {1,} *? ( {1,} ) - .

, .

Regex101:

, . , , .

,

P.S.: (<br\s*?\/?>) <br /> tags gm.

+4
1

:

<br\s*\/>(?=(?:(?!<\/?pre>)[\s\S])*<\/pre>)

. DEMO

:. br , </pre> <pre> </pre>

+3

All Articles