js之replace实现简单模板替换引擎

eg:

  var app={};

  app.tempEngine= (function () {
  var pattern = /{(w*[:]*[=]*w+)}(?!})/g;
  return function (template, json) {
  return template.replace(pattern, function (match, key, value) {
  return json[key];
     });
   }
})();

var template='<div>{name}</div>

<div>{ege}</div>

',data={

  name:"dabingzi",

      ege:28

}

$(app.tempEngine(template, data));

...../

...../

<div>dabingzi</div>

<div>28</div>

原文地址:https://www.cnblogs.com/dabingzi/p/5532222.html