I want to replace all matched text with another text, but I don't want replace if that text is in the alt
or href
attribute.
Example:
<p>Hello world!</p>
<p><img src="hello.jpg" alt="Hello"/></p>
Hello
My code:
var replacepattern = new RegExp('Hello', 'gi');
newcontent = newcontent.replace(replacepattern, function(match, contents, offset, s) {
var link = 'demo.com'
index++;
if (link != '') {
return '<a href="' + link + '">' + match + '</a>';
} else {
return match;
}
});
It works perfect with text only. How can I match text except img src
, alt
etc?