//使用正则表达式进行模板中定义的变量值替换,string.replace的高级用法;
//template为字符串,data为name value数组. 将template中${varname} 表示的字符串用 vars[varname]的值来替换
function TemplateReplace(template,vars){
return template.replace(/\${([^}]+)}/g,function(match,group){
name = group.toLowerCase();
return vars[name];
});
};
//usage:
tpl = 'hello ${name}';
result = templateReplace(tpl,{name:'amoruso'});
//result = 'hello amoruso'