模板数据
let templateStr = 'i am {{name}},age {{age}},job {{job}} ';
let data = {
name:'xbd',
age:18,
job:'CTO'
}
实现方法
function templateFunc(str,data){
let computed = str.replace(/\{\{(\w+)\}\}/g,function (match,key) {
console.log(match,key,123)
return data[key];
})
return computed;
}
console.log(templateFunc(templateStr,data));
输出
{{name}} name 123
{{age}} age 123
{{job}} job 123
i am xbd,age 18,job CTO