Here you can find the source of format()
/**//ww w. j a v a2 s. co m * Convenience method for assembling strings from multiple parts. You can pass in a set of strings as arguments * that will replace tokens in the source strings based on argument index, OR you can pass in an object * of name value pairs that will replace tokens in the string based on key values. * ex: '{0} {0} {1} {2}'.format(3.14, 'abc', 'foo'); //outputs: 3.14 3.14 abc foo * ex: '{key1} {name} {key1}'.format({"key1" : 123, "name" : "Bob"}); //outputs: 123 Bob 123 */ String.prototype.format = function() { var txt = this; var i = arguments.length; var replaceTokens = function(txt, key, value) { return txt.replace(new RegExp('\\{' + key + '\\}', 'gm'), value); }; if(i > 0 && typeof(arguments[0]) == "object") { //process name value pairs for(var key in arguments[0]) { txt = replaceTokens(txt, key, arguments[0][key]); } } else { //do replacement by argument indexes while (i--) { txt = replaceTokens(txt, i, arguments[i]); } } return txt; };
String.prototype.format = function() { var formatted = this; for (arg in arguments) { formatted = formatted.replace('{' + arg + '}', arguments[arg]); return formatted; };
String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function(m, i) { return args[i]; }); }; String.prototype.encodeHTML = function () { var s = this.valueOf(); ...
String.format = function () { var s = arguments[0]; for (var i = 0; i < arguments.length - 1; i++) { var reg = new RegExp("\\{" + i + "\\}", "gm"); s = s.replace(reg, arguments[i + 1]); return s; String.prototype.format = function () { ...
String.prototype.format = function () { var args = arguments; var reg = /\{(\d+)}/g; return this.replace(reg, function (g0, g1) { return args[+g1]; }); }; var str = "this is :{0} and {1} , and {0}".format("arg1","arg2") var main = "Scripts/main"; ...
String.prototype.format = function(){ var re_all = /{(\d+)?(:(\d+)?(.\d+?f))?}/g; var re_index = /(\d+):/; var re_before = /(\d+)\./; var re_after = /\.(\d+)/; var cur = this; var matches = cur.match(re_all); if (arguments.length > matches){ console.log("Too many arguments!"); ...
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(){ if(arguments.length==0) return this; var args=[], i=0; for(;i<arguments.length;i++) args[i]=arguments[i]; return this.replace(/\%[sSfdDTt\%]/g, function(m){ if(m=='%%') return '%'; var v=args.shift(); switch(m.substr(1,1)){ case 's': return v.toString(); ...
String.prototype.format=function(){ if(arguments.length==0) return this; var args=[], i=0; for(;i<arguments.length;i++) args[i]=arguments[i]; return this.replace(/\%[sSfdDTt\%]/g, function(m){ if(m=='%%') return '%'; var v=args.shift(); switch(m.substr(1,1)){ case 's': return v.toString(); ...
String.prototype.format = function (){ var values = [].slice.call(arguments, 0, arguments.length), idx, length = values.length, regex, str = this; if(str.match(/\{\d+\}/g).length !== length){ throw{ name: 'ArgumentMissMatch', ...