could you tell me why the replacement is not successful?
print message:
name
/ ${name} / "xiaoming"
the age of ${name} is ${age}
age
/ ${age} / 8
the 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);
}
}
}