Skip to content

Instantly share code, notes, and snippets.

@supasympa
Created September 26, 2013 08:26
Show Gist options
  • Save supasympa/6711373 to your computer and use it in GitHub Desktop.
Save supasympa/6711373 to your computer and use it in GitHub Desktop.
Replace a string with model object properties
var M = function(){
}
M.prototype = {
format : function(){
var s = "/data/mu/programs/{{programCode}}/campaign-mus/{{muid}}/validate";
var r = /{{[a-zA-Z_-]*}}/gi.exec(s);
var key = '';
while(r!==null){
key = r[0].replace('{{','').replace('}}','');
s = s.replace(r[0], this[key]);
r = /{{[a-zA-Z_-]*}}/gi.exec(s);
}
return s;
}
}
var m = new M();
m.programCode = 'YETI';
m.muid = 9999;
m.format();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment