zchfeng / Front-End-note

1 stars 0 forks source link

js模板引擎 #1

Open zchfeng opened 2 years ago

zchfeng commented 2 years ago
var template = "我的名字{{name}},性别{{sex}},年龄{{age}}";
var data = {
  name: "jeck",
  sex: 1,
};
function render(template, data) {
  return template.replace(/\{\{(\w+)\}\}/g, function (match, $1, index, str) {
    return data[$1];
  });
}
render(template, data)