Skip to content

Instantly share code, notes, and snippets.

@demoive
Last active December 16, 2015 20:58
Show Gist options
  • Save demoive/5495882 to your computer and use it in GitHub Desktop.
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}}.
/**
* 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