Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nossila/779045 to your computer and use it in GitHub Desktop.
Save nossila/779045 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