Skip to content

Instantly share code, notes, and snippets.

@stemar
Last active August 29, 2015 14:16
Show Gist options
  • Save stemar/a0a13a933b9b4ba33cd6 to your computer and use it in GitHub Desktop.
Save stemar/a0a13a933b9b4ba33cd6 to your computer and use it in GitHub Desktop.
JavaScript function that replaces named placeholders in a string template. http://jsfiddle.net/mobc4vss/8/
// Usage: "abc{def}ghi".p({def:"xyz"}); => "abcxyzghi"
String.prototype.p = function(obj) {
return this.replace(/\{[^\}]+\}/g, function(key) {
return obj[key.replace(/^\{([^\}]+)\}$/, "$1")] || key;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment