/ Published in: JavaScript

Example:
`var result = "Hello {0}! This is {1}.".format("world","foo bar");`
Returns:
`"Hello World! This is foo bar."`
`var result = "Hello {0}! This is {1}.".format("world","foo bar");`
Returns:
`"Hello World! This is foo bar."`
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
String.prototype.format = function(){ var pattern = /\{\d+\}/g; var args = arguments; return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; }); }
Comments
