Skip to content

Instantly share code, notes, and snippets.

@johnhunter
Created April 17, 2012 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhunter/2405443 to your computer and use it in GitHub Desktop.
Save johnhunter/2405443 to your computer and use it in GitHub Desktop.
Simple string interpolation
// 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