請問下為什么沒替換成功?打印信息為:name/${name}/ 'xiaoming'the age of ${name} is ${age}age/${age}/ 8the age of ${name} is ${age}const template = "the age of ${name} is ${age}";const data = { name: "xiaoming", age: 8};console.log(render(template, data));// 輸出: "the age of xiaoming is 8"function render(template,data) { for (key in data) { if(key) { console.log(key); var re = new RegExp("\$\{"+key+"\}"); console.log(re,data[key]); var ans = template.replace(re,data[key]); // console.log("test:",template.replace("${name}","xiaoming")); console.log(ans); } }}
正則表達式實現(xiàn)render函數(shù)
富國滬深
2019-03-30 11:19:32