Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Created September 13, 2013 08:46
Show Gist options
  • Save miraculixx/6548200 to your computer and use it in GitHub Desktop.
Save miraculixx/6548200 to your computer and use it in GitHub Desktop.
Provide a generic format method for JavaScript strings. Use see fiddle http://jsfiddle.net/UkxE7/
String.format = function(string) {
args = arguments.length == 2 ? arguments[1] : arguments.slice(1);
return string.replace(/{(\w+)}/g, function(match, idx) {
return typeof args[idx] != "undefined" ? args[idx] : match;
});
};
String.prototype.format = function() {
return String.format(this, arguments);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment