Replace multiple strings with multiple other strings
Specific Solution You can use a function to replace each one. var str = “I have a cat, a dog, and a goat.”; var mapObj = { cat:”dog”, dog:”goat”, goat:”cat” }; str = str.replace(/cat|dog|goat/gi, function(matched){ return mapObj[matched]; }); jsfiddle example Generalizing it If you want to dynamically maintain the regex and just add future exchanges … Read more