const data = {
a: '小aa',
b: '小bb',
c: {
cc: '小cc'
}
}
const tmpl = '哈哈,我是${a},你叫${b},他是${c.cc}'
function render (tmpl, data) {
const getData = (key) => {
let temp = data
key.split('.').forEach((item) => {
temp = temp[item]
})
return temp
}
return tmpl.replace(/${(.*?)}/g, (match, key) => {
return getData(key)
})
}
console.log(render(tmpl, data))
// 哈哈,我是我是小aa,你叫我是小bb,他是我是小cc