Regex to replace a string in HTML but not within a link or heading

Posted by Vladimir on Stack Overflow See other posts from Stack Overflow or by Vladimir
Published on 2010-06-10T09:26:49Z Indexed on 2010/06/10 9:33 UTC
Read the original article Hit count: 173

Filed under:

I am looking for a regex to replace a given string in a html page but only if the string is not a part of the tag itself or appearing as text inside a link or a heading.

Examples:

Looking for 'replace_me'

<p>You can replace_me just fine</p> OK

<a href='replace_me'>replace_me</a> no match

<h3>replace_me</h3> no match

<a href='/test/'><span>replace_me</span></a> no match

<p style="background:url('replace_me')">replace_me<h1>replace_me</h1></p> first no match, second OK, third no match

Thanks in advance!

© Stack Overflow or respective owner

Related posts about regex