Created
April 17, 2017 15:23
-
-
Save Hexodus/80252d484baaa64aff7bd8571e72ff0b to your computer and use it in GitHub Desktop.
Map Replace function. Replace multiple keys inside string with multiple values http://stackoverflow.com/a/15605648/1600193
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
String.prototype.map_replace = function (values) { | |
var string = this, key; | |
for (key in values) { | |
string = string.replace(new RegExp('\\{' + key + '\\}', 'gm'), values[key]); | |
} | |
return string | |
} | |
//usage | |
var teststring = "Mother and father loves much each other!"; | |
teststring.map_replace({father: "postman", much: "every day"}); | |
//output: Mother and postman loves every day each other! ;) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment