Created
April 17, 2012 11:33
-
-
Save johnhunter/2405443 to your computer and use it in GitHub Desktop.
Simple string interpolation
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
// use: String.subs('There is no ${foo} foo', { foo: 'bar' }) | |
String.subs = String.subs || function (str, props) { | |
return str.replace(/\$\{([^{}]*)}/g, function (a, b) { | |
var r = props[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