Replace vars in a string with Javascript
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
// Usage
// Pass in an object with the vars correspondent to those on the original string
"The date today is {todayDate}, thank you {name}".supplant({
"todayDate" : new Date().toString(),
"name" : "Clark Kent"
});