JS 模板字符串的简单实现
wengjq opened this issue · comments
let templateStr = 'I am ${{name}}, age ${{age}}, job ${{job}} ';
let data = {
name: 'wengjq',
age: 25,
job: 'fe'
}
function templateFunc(str, data) {
let computed = str.replace(/\$\{\{(\w+)\}\}/g, function (match,key) {
return data[key];
});
return computed;
}
console.log(templateFunc(templateStr, data)); // I am wengjq, age 25, job fe