Skip to content

Instantly share code, notes, and snippets.

@Kein1945
Created August 31, 2013 23:31
Show Gist options
  • Save Kein1945/6401324 to your computer and use it in GitHub Desktop.
Save Kein1945/6401324 to your computer and use it in GitHub Desktop.
Javascript String format
String.prototype.format = function() {
var i = -1, args = arguments;
return this.replace(/#\{(.*?)\}/g, function(one, two) {
return (typeof args[0] == 'object')?args[0][two]:args[++i];
});
}
var thing = 'world!';
console.log(
'hello #{1} - #{2}'.format(thing, 'two!'),
'hello #{wrd} - #{2}'.format({'wrd': thing, '2': 'two!'})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment