Skip to content

Instantly share code, notes, and snippets.

@SergSlon
Created September 2, 2013 10:30
Show Gist options
  • Select an option

  • Save SergSlon/6411475 to your computer and use it in GitHub Desktop.

Select an option

Save SergSlon/6411475 to your computer and use it in GitHub Desktop.
printf analog in JavaScript From http://habrahabr.ru/post/192124/
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