Created
December 1, 2008 18:56
-
-
Save kangax/30804 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 'My name is #{name=Earl}'.interpolate2(); // "My name is Earl" | |
| 'My name is #{name}'.interpolate2({ name: 'Earl2D2' }); // "My name is Earl2D2" | |
| */ | |
| String.prototype.interpolate2 = (function(){ | |
| var re = /#{([-\w]+)(?:=([-\w]+))?}/g; | |
| return function(source) { | |
| if (!source) source = { }; | |
| return this.replace(re, function(match, name, value) { | |
| return (name in source ? source[name] : (value || '')); | |
| }) | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment