I’ve converted html to a string, I’m able to use replace in that string to wrap the text with a link, and I can put that html back into the ID it came from.
My problem is that my replace method is going inside existing links on the page. This could create nested links, which is a problem. Does anyone out there know how to prevent the replace method from matching text that is within a link already?
I have right now:
keyword = "matching phrase";
keywordLink = "<a href='http://myurl.com'/>" + keyword + "</a>";
sasser = sasser.replace(keyword, keywordLink);
sasDom.innerHTML = sasser;
I’m looking for, in pseudo code:
... (keyword [if the next " < " sign is not followed by "/a>", regardless of how far away it is], keywordLink);
You can’t do this kind of thing with regex at all. Work on the document objects which are already nicely parsed into a structure for you.
Here’s a keyword linker adapted from this question.
eta re comments: Here’s a version of the same thing using plain text matching rather than regex: