Ivan Buttinoni
Ivan Buttinoni

Reputation: 4145

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:

Upvotes: -4

Views: 57

Answers (1)

Onyambu
Onyambu

Reputation: 79218

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.

Upvotes: -1

Related Questions