Skip to content

Instantly share code, notes, and snippets.

@rmariuzzo
Created February 2, 2014 01:16
Show Gist options
  • Save rmariuzzo/8761698 to your computer and use it in GitHub Desktop.
Save rmariuzzo/8761698 to your computer and use it in GitHub Desktop.
Simple and minimal `sprintf` function in JavaScript.
function sprintf(format) {
var args = Array.prototype.slice.call(arguments, 1);
var i = 0;
return format.replace(/%s/g, function() {
return args[i++];
});
}
@kevinfilteau
Copy link

Nice! Thanks

@r4phf43l
Copy link

r4phf43l commented Nov 8, 2020

I love it!

const sprintf = ( format, ...args ) => ( i = 0, format.replace( /%s/g, () => args[i++] ) );

My version in arrow function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment