Skip to content

Instantly share code, notes, and snippets.

@skylying
Created November 26, 2016 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skylying/71f855c6a6a3a1f6a68c77e97b9c29d1 to your computer and use it in GitHub Desktop.
Save skylying/71f855c6a6a3a1f6a68c77e97b9c29d1 to your computer and use it in GitHub Desktop.
Template syntax in javascript
var template = "<div><h1>{{title}}</h1></div> <div><h1>{{name}}</h1></div>"
var content = {
"title": 'Hello world'
"name": 'Jack'
}
function replacer(match, p1, offset, string) {
for (var key in content) {
if (p1.indexOf(key) != '-1') {
p1 = content[key]
};
};
// p1 is nondigits, p2 = digits, p3 = non-alphanumerics
return p1;
};
replaced = template.replace(/(\{\{[\S]*\}\})/g, replacer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment