-
-
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/
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
// 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