Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <h2>{title}</h2>
- <p><a href="{url}">{content}</a></p>
- ['title', 'url', 'content']
- var pattern = new RegExp("{[a-zA-Z]+}+");
- var match = pattern.exec("{Sample} bob {text}");
- var pattern = new RegExp("{[a-zA-Z]+}", "g");
- var pattern = /{[a-zA-Z]+}/g;
- var matches = "{Sample} bob {text}".match(pattern);
- var pattern = new RegExp("{[a-zA-Z]+}+", "g");
- <script>
- var text = '<h2>{title}</h2>n<p><a href="{url}">{content}</a></p>';
- var regex = /{[a-z]+}/ig;
- var result = text.match(regex);
- for (var i = 0; i < result.length; i++) {
- console.debug(i + ". " + result[i]);
- }
- /*
- gives:
- 0. {title}
- 1. {test}
- 2. {url}
- 3. {content}
- */
- </script>
- String.prototype.template = function (obj) {
- return this.replace(/{([^{}]+)}/g,
- function (full, word) {
- return((typeof obj[word]==='string'||typeof obj[word]==='number')?obj[word]:full);
- }
- );
- };
- "The {adj1} {adj2} {noun}.".template({adj1: 'lazy',adj2: 'brown', noun: 'dog'})
- ==> "The lazy brown dog."
Add Comment
Please, Sign In to add comment