Here you can find the source of format()
/**//from w w w. j av a 2 s. c om * String.format function * * see: http://stackoverflow.com/questions/4974238/javascript-equivalent-of-pythons-format-function * usage: "The lazy {} {} over the {}".format("dog", "jumped", "foobar"); * outputs: * "The lazy dog jumped over the foobar" */ String.prototype.format = function () { var i = 0, args = arguments; return this.replace(/{(.*?)}/g, function (match, tagInner) { replacement = typeof args[i] != 'undefined' ? args[i++] : ''; if(tagInner == "") { return replacement; } else { var match = tagInner.match(/^:.(\d+)f$/); if(match) { precision = parseInt(match[1]); return parseFloat(replacement).toFixed(precision); } else { return replacement } } }); };
String.prototype.printf = function (obj) { var useArguments = false; var _arguments = arguments; var i = -1; if (typeof _arguments[0] == "string") { useArguments = true; if (obj instanceof Array || useArguments) { return this.replace(/\%s/g, ...
String.prototype.format = function () { 'use strict'; var newString = this; function replace(string, search, replacement) { return string.replace(new RegExp(search.replace('{', '\\{').replace('}', '\\}'), 'g'), replacement.toString()); if (arguments[0] !== 'undefined') { switch (typeof arguments[0]) { case 'object': ...
String.prototype.format = function() { var args = Array.prototype.slice.call(arguments); var str = this; return str.replace(/%s/g, function(match, index) { if (str.charAt(index-1) == "%") { return match; } else { return args.shift(); });
if (!String.prototype.format) { String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); ...
String.prototype.format = function () { var args = arguments; return this.replace(/\{(\d+)\}/g, function (m, i, o, n) { return args[i]; }); };
String.prototype.format = function() { var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); return s; };
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; }; ...