Last active
December 16, 2015 20:58
-
-
Save demoive/5495882 to your computer and use it in GitHub Desktop.
Variable substitution on a string.
Modified from Douglas Crockford's to use {{double curly braces}}.
This file contains 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
/** | |
* Variable substitution on a string. | |
* | |
* Scans through a string looking for expressions enclosed within double curly braces. | |
* If an expression is found, use it as a key on the object, | |
* and if the key has a string or number value, | |
* it is substituted for the bracket expression and it repeats. | |
* | |
* Originally by Douglas Crockford: http://javascript.crockford.com/remedial.html | |
*/ | |
if (!String.prototype.supplant) { | |
String.prototype.supplant = function (o) { | |
return this.replace( | |
/{{([^{}]*)}}/g, | |
function (a, b) { | |
var r = o[b]; | |
return typeof r === 'string' || typeof r === 'number' ? r : a; | |
} | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment