evilmolen
evilmolen

Reputation: 31

RegEx to match a string in a HTML tag not containing specific class

I want to use Regex to find all words matches in HTML code and then replace them with the links, but I need to exclude tags with specific class from search. In this example I need to find all words as:

"awesome"

and exclude from search all tags with class:

goaway

And finally replace them with (only inside tags, not in titles or ALTs attributes) my link:

<a href="mysite.com">awesome</a>

Source HTML code:

<p> 
    this is awesome text about my cat 
    <img src="cat.jpg" title="my awesome cat"/>
</p>
<p class="goaway">
    I don't need this awesome match 
</p>
<div>
    and this is element of my awesome code
</div>
<span class="goaway">
    That isn't awesome word
</span>

I am doing replacements with preg_replace_callback() function, and this is what I want to see as result:

<p>
    this is <a href="mysite.com">awesome</a> text about my cat
    <img src="cat.jpg" title="my awesome cat"/>
</p>
<p class="goaway">
    I don't need this awesome match 
</p>
<div>
    and this is element of my <a href="mysite.com">awesome</a> code
</div>
<span class="goaway">
    That isn't awesome word
</span>

Right now everything what I achieved is a pattern to ignore attributes content

"|(?!<.*?)awesome(?![^<>]*?>)|"

So, I need your help with completing the RegEx pattern. Thanks!

P.S. English, do I speak it?.. Nope, and I'm so sorry for that, but I hope you can understand my thoughts

Upvotes: 0

Views: 2910

Answers (1)

Kaspar Lee
Kaspar Lee

Reputation: 5596

Try this RegEx:

(<(?!\s+class="goaway")[^>]*>[\w\n\s]*)(awesome)

And replace like this:

\1<a href="mysite.com">awesome</a>

Live Demo on Regex101

Upvotes: 2

Related Questions