Steelclover Steelclover - 1 year ago 68
Javascript Question

Make a mountain out of a molehill by replacing it with JavaScript

I want to replace multiple words on a website with other words. That is, I am interested in finding all instances of a

word and replacing it with a

Sample Cases:

Source | Target

Molehill => Mountain

Green => Grey

Google =>
<a href="">

Sascha => Monika

Football => Soccer

Answer Source

Here is the JS:

function replaceWords () {
var toReplace = [
    ["Google","<a href=''>"]
var input = document.getElementById("content").innerHTML;
console.log("Input: " + input);
for (var i = 0; i < toReplace.length; i++) {
    var reg = new RegExp(toReplace[i][0],"g");
    input = input.replace(reg,toReplace[i][1]);
document.getElementById("content").innerHTML = input;



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download