Skip to content

Instantly share code, notes, and snippets.

@jonathanconway
Created August 24, 2014 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonathanconway/6afae9880e4b18ca5c61 to your computer and use it in GitHub Desktop.
Save jonathanconway/6afae9880e4b18ca5c61 to your computer and use it in GitHub Desktop.
Straight-forward string.prototype format method for Javascript.
// Credit: http://joquery.com/2012/string-format-for-javascript
String.prototype.format = function() {
var s = this;
for (var i = 0; i < arguments.length; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i]);
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment