This seems simple but finding it really hard to suss out tried a tonne of stuff.
Basically I want to search the dom with js and find every instance of a word and replace it with another one. This is easy to do.
function replaceAllText(text) {
var walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT,
null,
false
);
while (walker.nextNode()) {
walker.currentNode.innerHTML = walker.currentNode.innerHTML.replaceAll(/hello/ig, text);
}
}
replaceAllText('Holla');
The hard bit is I want to do it only on certain tags for instance I dont want it to replace text inside A tags.
Now I thought skipping A tags would be easy I could do it like this.
function replaceAllText(text) {
var walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_ELEMENT,
null,
false
);
while (walker.nextNode()) {
if (walker.currentNode.tagName !== 'A') {
walker.currentNode.innerHTML = walker.currentNode.innerHTML.replaceAll(/hello/ig, text);
}
}
}
replaceAllText('Holla');
This still places the text inside A tags.
I have an example here I am testing with https://jsbin.com/deculekogu/2/edit?html,js,console,output
The result I am looking to achieve is to convert this.
To this see its replacing all instances of hello with holla except inside the A tag.

