Return to Snippet

Revision: 8910
at October 13, 2008 18:42 by jasonseney


Initial Code
String.prototype.format = function(){
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
}

Initial URL

                                

Initial Description
Example:

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

Returns: 

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

Initial Title
Sprintf in Javascript (string format)

Initial Tags
javascript, format

Initial Language
JavaScript