Last active
February 13, 2019 07:24
-
-
Save melvinstanly/4e0574aa928880a0516a7f11bf768c64 to your computer and use it in GitHub Desktop.
Replace multiple strings in a single run in js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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