I'm writing a jquery script that is to be run in a large number of websites all written in different ways.
My intention is to place certain html tags around bits of text within the code - sounds simple? But it isn't.
One approach which I've tried is to use is this:
$('body').ready(function() {
var bodyHTML = $('body').html();
var matches = bodyHTML.match(/stringtomatch/g); // array of all matches (note actual regex is much more complicated and matches a large variety of strings not just this)
var match;
for (var i = 0; i<matches.length; i++) {
match = matches.pop();
bodyHTML = bodyHTML.replace(match, '<span id="blah">'+match+'</span>'); // replace each in body
}
$('body').html(bodyHTML); // set new body back to DOM
});
this replaces the entire content of the body which in itself is inefficient because there's a lot of unreplaced stuff having to be added back to the DOM, but also it fails flat for a large amount of sites and for some reason when the html is set back on there is a lot less html than there is supposed to be. I'm not sure as to why.
It would be great if there was some way I could just find the text within the entire DOM and add tags wherever the string matching my regular expression is found but after several hours of research I haven't found anything.
Any ideas?