javascript replace globally with array

var array = {"from1":"to1", "from2":"to2"}

for (var val in array)
    text = text.replace(new RegExp(val, "g"), array[val]);

Edit: As Andy said, you may have to escape the special characters using a script like this one.

Leave a Comment