Posted By

Pjotor on 04/30/10


replace pattern String

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Pattern replace String prototype

 / Published in: JavaScript

Quick and dirty pattern string replacer. Usage: "my {color} {fruit}".prep({ color: "red", fruit: "apple"})

  1. String.prototype.prep = function(d){ var s = this; for(var i in d) { s = s.replace('{'+i+'}', function($1){ return d[$1.substr(1,$1.length-2)]; }); } return s; }

Report this snippet  

You need to login to post a comment.