Skip to content

Instantly share code, notes, and snippets.

@Hexodus
Created April 17, 2017 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexodus/80252d484baaa64aff7bd8571e72ff0b to your computer and use it in GitHub Desktop.
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
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