Sprintf in Javascript (string format)


/ Published in: JavaScript
Save to your folder(s)

Example:

`var result = "Hello {0}! This is {1}.".format("world","foo bar");`

Returns:

`"Hello World! This is foo bar."`


Copy this code and paste it in your HTML
  1. String.prototype.format = function(){
  2. var pattern = /\{\d+\}/g;
  3. var args = arguments;
  4. return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
  5. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.