Skip to content

Instantly share code, notes, and snippets.

@darthwade
Created March 2, 2014 18:15
Show Gist options
  • Save darthwade/9310975 to your computer and use it in GitHub Desktop.
Save darthwade/9310975 to your computer and use it in GitHub Desktop.
JS String.format, String.prototype.format
// "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
// String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET');
if (!String.format) {
String.format = function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment