Hexodus
4/17/2017 - 3:23 PM

Map Replace function. Replace multiple keys inside string with multiple values http://stackoverflow.com/a/15605648/1600193

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! ;)