Search code examples
regexregex-look-ahead

Regexp: negative lookahead that don't allow an open tag inside a tag


I'm looking for a negative lookahead that don't allow an open tag inside a tag, I try

failing negative lookahead #1

/(<(\w+)[^>]*>)((?!<\2).*?)(<\/\2>)/gs

see the example

failing negative lookahead #2

/(<(\w+)[^>]*>)((?!<\2).*)(<\/\2>)/gs

see the example

alpha
<div>
alpha<div>
beta<div>
x < y divided by 4
</div>
</div>
</div>


<div>
    <span style="font-size: 8pt;" disabled title="data">
        <span>
          infinite
        </span>
        <?= $record->id ?>
    </span>
    <div> equal </div>
</div>


<div> sum </div>

When x y and y > 0 
<div  style="font-size: 8pt;" >Summary</div> 
Equation id <?= $equation->id ?>

In this exampled they're the once containing:

  • x < y divided by 4
  • infinite
  • equal
  • sum
  • summary

Solution

  • Here is a regex you may want to use:

    .*?<(\w+)[^>]*>((?:(?!<\1>).)*?)<\/\1>|.*

    Click on it for explanation and also to see how to use it.