Skip to content

Instantly share code, notes, and snippets.

@kinncj
Last active August 12, 2016 14:44
Show Gist options
  • Select an option

  • Save kinncj/77113de91a276d80e5a4c58bd2bca187 to your computer and use it in GitHub Desktop.

Select an option

Save kinncj/77113de91a276d80e5a4c58bd2bca187 to your computer and use it in GitHub Desktop.
String.replaceMap(parameterList);
"hello world".replaceMap({
" ": ", ",
"wor": "it's",
"ld": " me"
});
"hello, it's me"

Test

Copy and paste to the console:

String.prototype.replaceMap = function(parameters) {
    var string = this.toString();
    Object.keys(parameters).forEach(function(key){
        string = string.replace(new RegExp(key, 'g'), parameters[key]);
    });

    return string;
}

"hello world".replaceMap({
    " ":   ", ",
    "wor": "it's",
    "ld":  " me"
});
String.prototype.replaceMap = function(parameters) {
var string = this.toString();
Object.keys(parameters).forEach(function(key){
string = string.replace(new RegExp(key, 'g'), parameters[key]);
});
return string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment