美文网首页
模板解析

模板解析

作者: 六寸光阴丶 | 来源:发表于2020-10-09 17:16 被阅读0次

模板解析函数

const render = (str = "") => (obj = {}) => str.replace(/\$\{(\w+)\}/g, (_, key) => obj[key])

测试

const str = render('${year}-${month}-${day}')({
  year: '2020',
  month: '09',
  day: '23'
})
console.log(str)

测试结果

2020-09-23

相关文章

网友评论

      本文标题:模板解析

      本文链接:https://www.haomeiwen.com/subject/oiolpktx.html