Skip to content

Latest commit

 

History

History
16 lines (13 loc) · 360 Bytes

1.md

File metadata and controls

16 lines (13 loc) · 360 Bytes

模版字符串封装

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);