A language such as PHP (or which uses PCRE) has a \K
token which means "reset the match so far." That means that you can very specifically indicate the text that should occur before the part you want to match, reset the match, and your "regex cursor" will start just after that portion.
You can see this example here where the <p>
element is found first, and once any other <...>
elements are matched, the \K
is used to reset the match. As you can see, the captured text only highlights if SomePhrase1
exists.
EDIT:
There are many edge cases that you may have to account for, where XML/HTML just utterly fail:
<p class='testing1_class'><span>Lorem Ipsum SomePhrase1 Lorem Lorem Lorem</span></p>
<p class='testing2_class'><span>Lorem Ipsum SomePhrase2 Lorem Lorem Lorem</span></p>
<p class='testing1_class'><span>Lorem Ipsum SomePhrase1 Lorem Lorem Lorem</span></p>
<span><p class="testing2_class"><p>Lorem Ipsum SomePhrase1 Lorem Lorem Lorem</p></p></span>
Lorem Ipsum SomePhrase1 Lorem Lorem Lorem
<span class='testing1_class'>Lorem Ipsum SomePhrase1 Lorem Lorem Lorem</span>
<p>Lorem Ipsum SomePhrase1 Lorem Lorem Lorem</p>
<p style='color: black;' class='foo bar testing1_class baf' id='#magic'>Lorem Ipsum SomePhrase1 Lorem Lorem Lorem</p>
<p class='testing1_class'>Lorem Ipsum <span>SomePhrase1</span> Lorem Lorem Lorem</p>
<p class='testing1_class'>Lorem Ipsum Lorem Lorem Lorem</p>
<p class='testing1_class'>Lorem <p>Ipsum SomePhrase1 Lorem</p> Lorem Lorem</p>
<p class='testing1_class'>SomePhraseX</p><p class='testing1_class'>WrongPhrase</p><p class='testing1_class'>Another Wrong Phrase</p>
The regex to handle all these cases is very fragile and will become very complicated. jQuery would allow you to access it MUCH simpler, however: JSFIDDLE