function template(tpl, data, translation = true) {
return tpl.replace(/{.*?}/g, function (str) {
let res = data[str.replace(/[{}]/g, '')];
if (res !== undefined && translation) {
res = res.replace(/[<>]/g, function (str) {
if (str === '<') return '<';
if (str === '>') return '>';
})
}
return res;
})
}
let tpl = "<p>這是{name}的第一個{content}</p>";
let data = {
name: 'xxxx',
content: "<script> alert( 'mmmmmm' ); </script>"
}
let f = template(tpl, data);
console.log(f)