Skip to content

Instantly share code, notes, and snippets.

@daGrevis
Created May 2, 2012 12:49
Show Gist options
  • Select an option

  • Save daGrevis/2576324 to your computer and use it in GitHub Desktop.

Select an option

Save daGrevis/2576324 to your computer and use it in GitHub Desktop.
String format in JavaScript
String.prototype.format = function () {
var g = this.toString();
if (!arguments.length) return g;
var c = "string" == typeof arguments[0] ? arguments : arguments[0],
e;
for (e in c) g = g.replace(RegExp("\\{" + e + "\\}", "gi"), c[e]);
return g
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment