This is a follow-up of the thread at Replace text in a website.
The top answer given by @Paulpro on replacing texts in a website works like a charm, but I do not know how to use regular expressions in the last line:
replaceTextOnPage('original text', 'new text')
.
I tried to use something like
replaceTextOnPage(/(?!bingogame)bingo/g, 'bridge')
to replace the text 'bingo'
with 'bridge'
(excluding matches found in 'bingogame'
) but the whole script was messed up by that:
function replaceTextOnPage(from, to){
getAllTextNodes().forEach(function(node){
node.nodeValue = node.nodeValue.replace(new RegExp(quote(from), 'g'), to);
});
function getAllTextNodes(){
var result = [];
(function scanSubTree(node){
if(node.childNodes.length)
for(var i = 0; i < node.childNodes.length; i++)
scanSubTree(node.childNodes[i]);
else if(node.nodeType == Node.TEXT_NODE)
result.push(node);
})(document);
return result;
}
function quote(str){
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
}
replaceTextOnPage(/(?!bingogame)bingo/g, 'bridge')
Can someone tell me how to incorporate regular expression correctly? Pardon my stupidity; grandma talking here.