This is meant as a complex comment on elclarns great answer suggesting these alternatives:
- May be written as
String.prototype - You can use
arguments
Function can be changed to
String.prototype.format = function() { var args=arguments; return this.replace(/%(\d+)/g, function(_,m) { return args[--m]; }); }
And called this way
var result = "I am %1, %2 years old %1".format("Jan",32); // I am Jan, 32 years old Jan
source share