I'm working on a simple browser plug in to replace 1 word with another but with the addition of an anchor for a small tooltip/pop up. I have this actually working already but my problem is, if the word to be replaced is within an anchor already, then my </a>
closes the already open <a>
.
So I need some help with how to only replace a word, as long as its not within an open anchor tag. A follow up to this is to also make sure the target word isn't in an <input>
or other inappropriate tags.
My code is:
var regex = new RegExp("\\b" + nativeWord + "\\b", "igm");
var body = $('body');
body.each(function() {
$(this).html(
$(this).html().replace(regex, function() {
counter++;
if(counter > wordSwapLimit) {
return nativeWord;
} else {
return "<a class=\"tooltip\" href=\"#\">" + studyWord + "<span class=\"classic\">" + nativeWord + "</span></a>";
}
})
);
});
I'm suspecting that I might need to write a more complex RegExp but this is my first outing with it so any suggestions would be greatly appreciated!
Update So I have a test page I work with to see how my code works. This is part of the original HTML from the page:
<p id="changeArea"> I <a href="http://www.chicken.com">love chicken but I hate</a> beef. </p>
But with the code shown above and swapping 'chicken' for 'kip', this gets changed to:
<p id="changeArea"> I <a href="http://www.<a class=" tooltip"="">kip<span class="classic">chicken</span></a>.com">love <a class="tooltip" href="#">kip<span class="classic">chicken</span></a> but I hate beef. </p>
If I have no anchors around what I am swapping, then it works perfectly and I get a nice rollover tooltip.
Thanks again for your help!
Update 2 As requested, here are 'unprocessed' examples that my plugin might come across when swapping 'chicken' for 'kip':
<p id="changeArea"> I <a href="http://www.chicken.com">love chicken but I hate</a> beef. </p>
<p id="changeArea2"> Chickenpie? pie-chicken. </p>
What I'm hoping for is the following:
<p id="changeArea"> I <a href="http://www.chicken.com">love chicken but I hate</a> beef. </p>
<p id="changeArea2"> Chickenpie? pie-<a class="tooltip" href="#">kip<span class="classic">chicken</span></a>. </p>
As you can see in the first line, the html code is left alone, as is text for the word, because it is within a URL and so would break it if my tooltip anchor was put in. The second line ignores 'chickenpie' because I only want whole word swapping, as per my existing regexp. 'pie-chicken' does get swapped though. This line is processed correctly with existing code.
I'm literally just looking to add additional rules to my existing code that say 'dont replace code and dont replace if within an anchor open tag.