Javascript 如何制作角度模式绑定格式解析器?
我想做一个类似于绑定格式的解析。 例如:Javascript 如何制作角度模式绑定格式解析器?,javascript,regex,angularjs,Javascript,Regex,Angularjs,我想做一个类似于绑定格式的解析。 例如: var test = 'this is a test'; {{test}} // output: 'this is a test' {{go(test)}} // output: go('this is a test') {{test + ' yes'}} // output: 'this is a test yes' 我尝试下面的代码,但有没有这样的自由 templatePattern: function (template, item) {
var test = 'this is a test';
{{test}} // output: 'this is a test'
{{go(test)}} // output: go('this is a test')
{{test + ' yes'}} // output: 'this is a test yes'
我尝试下面的代码,但有没有这样的自由
templatePattern: function (template, item) {
var row = template;
for (var key in item) {
var regx = new RegExp("{{(?:.*\\()?" + key + "(?:\\).*)?}}", "gm");
var results = regx.exec(row);
if (results) {
var found = results[0].replace('{{', '').replace('}}', '');
found = found.replace(key, item[key]);
var data = '';
if (found == item[key])
data = found;
else
data = eval(found);
row = row.replace(regx, data)
}
}
return row;
},
为什么不直接使用handlebarsJS或其他什么呢?我有一个自定义格式和我自己的插件,我想自己编写。。