Skip to content

Instantly share code, notes, and snippets.

@dmiro
Last active December 17, 2015 00:09
Show Gist options
  • Save dmiro/5518597 to your computer and use it in GitHub Desktop.
Save dmiro/5518597 to your computer and use it in GitHub Desktop.
String format emulate
// Format emulate
String.prototype.format = function () {
var args = arguments;
return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
if (m == "{{") { return "{"; }
if (m == "}}") { return "}"; }
return args[n];
});
};
@dmiro
Copy link
Author

dmiro commented May 4, 2013

Example:

console.log("soy el numero {0}".format(1));
// return soy el numero 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment