模版字符串封装
function generateHtmlString(str, data) {
return str.replace(/\$\{([^}]+)\}/g, function(_, variableName) {
return data[variableName.trim()];
});
}
const animal = 'dog';
const age = 2;
const str = '<ul><li>${animal}</li><li>${age}</li></ul>';
const newStr = generateHtmlString(str, { animal, age });
console.log(newStr);