Open krasimir opened 4 years ago
var template = 'Hello, my name is <%name%>. I\\'m <%age%> years old.';
I\\'m
=>I\'m
var re = /<%([^%>]+)?%>/g;
[^%><]
would be better?The final implementation has errors:
1.
line.replace(/"/g, '\\\\"') + '");
this code syntaxError: missing ) after argument list
change:
line.replace(/"/g, '\\"') + '");
2. last line code to update
return new Function(code.replace(/\\r|\\t|\\n/g, '')).apply(options);
Article 👉 https://krasimirtsonev.com/blog/article/Javascript-template-engine-in-just-20-line
JavaScript template engine in just 20 lines