Here's the idiom for a simple, non-RegExp string replaced in JS, so you don't have to worry about special regex characters:
for (var val in array) text= text.split(val).join(array[val]);
Please note that there are problems using the object as a general purpose. If someone is monkeys with a prototype object (a bad idea, but some libraries do this), you can get more val s than you wanted; you can use the hasOwnProperty test to avoid this. Plus in IE, if your string came across an Object method, like toString , IE will mysteriously hide it.
In your example, you are fine here, but in the general case, when the strings can be anything, you will need to get around this, either by processing the key strings to avoid collisions, or using a different data structure such as an array of arrays [find, replace] .
bobince
source share