自己实现模版字符串

Posted apple78

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己实现模版字符串相关的知识,希望对你有一定的参考价值。

const obj = {
    name: ‘zj‘,
    age: 18
  }
const str = `我的名字叫${ obj.name },今年${obj.age}岁`

 function replace(str){
     return str.replace(/${([^}]+)}/, function(data){
         return data;
     });
 }

console.log(replace(str))
console.log(str) 
//
[^}]+ 匹配}符号,+代表一次或多次

 

以上是关于自己实现模版字符串的主要内容,如果未能解决你的问题,请参考以下文章