Javascript String render(obj)
'use strict';//from w ww. jav a2s . c o m String.prototype.render = function(obj) { return this.replace(/\{(\w+)\}/g, function(r, e) { return obj[e]; }); };
String.prototype.render = function(obj) { str = this;// w ww .j av a 2 s. c o m for (var key in obj) { var reg = new RegExp('\\$\\{' + key + '}'); str = str.replace(reg, obj[key]); } return str; } var greeting = 'my name is ${name},age ${age}'; var result = greeting.render({ name: 'XiaoMing', age: 11 }); console.log(result);