用es5实现模板字符串

废话不多说,主要是利用正则表达式replace+eval动态取值(纯属娱乐)

    String.prototype.myReplace = function(){
         return this.replace(/${([^}]*)}/g,function(metched,key){
            return eval(key)
        })
    }
    var name = '张三',age = '20';
    var userTxt = '${name}今年${age}岁了'


原文地址:https://www.cnblogs.com/angfl/p/11928292.html