1 function templateReplace(template, data) { 2 for (var i in data) { 3 oldRe = new RegExp('\\{\\$' + i + '\\}', 'g'); 4 newStr = data[i].toString(); 5 template = template.replace(oldRe, newStr); 6 } 7 return template; 8 } 9 10 // =======test======== 11 var data = { 12 name:'Zhao Kang', 13 age:'24' 14 }; 15 16 var template = "My name is {$name}, my age is {$age}." 17 18 templateReplace(template, data); // "My name is Zhao Kang, my age is 24."