interpolate function need
问题 I need an javascript function that can do interpolate like the one for prototype js framework. Anyone have an interpolate function that is not depend on prototype? Jquery is welcome. Thanks. 回答1: Depending on your needs, something like this might work: String.prototype.interpolate = function(valueMap){ return this.replace(/\{([^}]+)\}/g, function(dummy, v){ return valueMap[v]; }); }; Usage: var params = { "speed":"slow", "color":"purple", "animal":"frog" }; var template = "The {speed} {color}