Skip to content

Instantly share code, notes, and snippets.

@willywongi
Created January 12, 2011 14:10
Show Gist options
  • Save willywongi/776196 to your computer and use it in GitHub Desktop.
Save willywongi/776196 to your computer and use it in GitHub Desktop.
/* Python(ish) string formatting:
* >>> format('{0}', ['zzz'])
* "zzz"
* >>> format('{x}', {x: 1})
* "1"
*/
var format = (function() {
var re = /\{([^}]+)\}/g;
return function(s, args) {
return s.replace(re, function(_, match){ return args[match]; });
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment