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>
原标题:js之replace实现简单模板替换引擎
关键词:JS