Skip to content

Instantly share code, notes, and snippets.

@lot224
Created April 15, 2016 15:29
Show Gist options
  • Save lot224/ca1dc4520c671012e7fbc8d4b699fbc9 to your computer and use it in GitHub Desktop.
Save lot224/ca1dc4520c671012e7fbc8d4b699fbc9 to your computer and use it in GitHub Desktop.
javascript string.format like c# (ish)
if (!String.prototype.format) {
String.prototype.format = function () {
var n = this;
for (var i = 0; i < arguments.length; i++) {
var e = new RegExp('\\{' + (i) + '\\}', 'gm');
n = n.replace(e, arguments[i]);
}
return n;
}
}
if (!String.format) {
String.format = function () {
for (var i = 1; i < arguments.length; i++) {
var e = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
arguments[0] = arguments[0].replace(e, arguments[i]);
}
return arguments[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment