I'm trying to turn a set of keywords into links on my site. I am currently using this code which will turn one keyword into a link. However, now I want to expand it to have several words. The link will always be the same, however, the keyword changes, so the link text must also reflect that.
Here is the code I am currently using:
<script type="text/javascript">
(function($) {
var thePage = $("body");
thePage.html(thePage.html().replace(/Wedding Stationery/ig, '<a class="discrete" href="http://www.kateguest.com">wedding stationery</a>'));
})(jQuery)
</script>
How can I expand this to use 5 or 6 keywords?
I would suggest something like this:
Do the replacement in a loop. You can use
$&
in the replacement to refer to the text that was matched.DEMO