Here you can find the source of format(arg)
String.prototype.format = function(arg) { var argLen, args, repFn, str; repFn = void 0;//from w w w. ja v a 2 s . c o m str = this; if (typeof arg === "object") { repFn = function(a, b) { if (arg[b]) { return arg[b]; } else { return a; } }; } else { args = arguments; argLen = args.length - 1; str = str.replace(/(?!\{)*\{\{(\w+)\}\}/g, function(a) { var ret; args[++argLen] = a.replace("{{", "{").replace("}}", "}"); ret = "{" + argLen + "}"; return ret; }); repFn = function(a, b) { return args[parseInt(b, 10)]; }; } return str.replace(/(?!\{)*\{(\w+)\}/g, repFn); };
String.prototype.format = function () { var args = arguments; var input = this; var output = ""; if (input.length > 0) { if (args[0] === undefined || args[0] === null) { output = "string without any arguments"; else { ...
String.prototype.format = function() { var stringToReturn = this.toString(); for(var i = 0; i < arguments.length; i++) { var regex = new RegExp("\\{" + i + "\\}", "g"); stringToReturn = stringToReturn.replace(regex, arguments[i]); return stringToReturn; };
String.format = function() { "use strict" var theString = arguments[0]; for (var i = 1; i < arguments.length; i++) { var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm"); theString = theString.replace(regEx, arguments[i]); return theString; }; ...
'use strict'; String.prototype.format = function () { var content = this; for (var i = 0; i < arguments.length; i++) { var replacement = '{' + i + '}'; content = content.replace(replacement, arguments[i]); return content; }; ...
String.prototype.format = function (...args) { return this.replace(/\{(\d+)\}/g, function (m, i) { return args[i] })
var result = this; if (arguments.length > 0) { if (arguments.length == 1 && typeof (args) == "object") { for (var key in args) { if(args[key]!=undefined){ var reg = new RegExp("({" + key + "})", "g"); result = result.replace(reg, args[key]); else { for (var i = 0; i < arguments.length; i++) { if (arguments[i] != undefined) { var reg = new RegExp("({[" + i + "]})", "g"); result = result.replace(reg, arguments[i]); return result; };
String.prototype.format = function (args) { var newStr = this; for (var key in args) { newStr = newStr.replace('{' + key + '}', args[key]); return newStr;
String.prototype.format = function (args) { var newStr = this; for (var key in args) { newStr = newStr.replace(new RegExp("{" + key + "}","g"), args[key]); return newStr; };
String.prototype.format = function(args) { return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); };