Skip to content

Instantly share code, notes, and snippets.

@melvinstanly
Last active February 13, 2019 07:24
Show Gist options
  • Save melvinstanly/4e0574aa928880a0516a7f11bf768c64 to your computer and use it in GitHub Desktop.
Save melvinstanly/4e0574aa928880a0516a7f11bf768c64 to your computer and use it in GitHub Desktop.
Replace multiple strings in a single run in js
// Replace multiple strings with multiple values
var str = "I have a cat, a dog, and a goat.";
var mapObj = {
cat:"monkey",
dog:"goat",
goat:"cat"
};
str = str.replace(/cat|dog|goat/gi, function(matched){
return mapObj[matched];
});
alert(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment