Here you can find the source of format(args)
/**//w w w . j a va 2 s . c o m * Add a format method to the prototype of String Class * Inorder to achieve python's format method functionality * * Usage - * var formattedString = "hi $0 $1 $2 $3.".format(["pk", "bk", "ck", "dk"]); //hi pk, bk, ck, dk * * @param {array} [args] [list of strings, that has to be replaced] * @return {string} Returns the string with replaced text. */ String.prototype.format = function(args){ var str = this.toString(), result = str.match(/\$[0-9]+/gi), mapper = {}; if(args.length < result.length) throw new Error("Index out of range."); for(var i = 0; i < result.length; i++){ //create a mapper mapper[result[i]] = args[parseInt(result[i].replace("$", ""), 10)] } for(var i = 0; i < result.length; i++){ str = str.replace(result[i], mapper[result[i]]); } return str; };
String.prototype.format = function (args) { var str = this; return str.replace(String.prototype.format.regex, function(item) { var intVal = parseInt(item.substring(1, item.length - 1)); var replace; if (intVal >= 0) { replace = args[intVal]; } else if (intVal === -1) { replace = "{"; ...
'use strict'; String.prototype.format = function (args) { if (arguments.length > 0) { var result = this; if (arguments.length == 1 && typeof (args) == "object") { for (var key in args) { result = result.replace(new RegExp("({" + key + "})", "g"), args[key]); else { for (var i = 0; i < arguments.length; i++) { if (arguments[i] == undefined) { return ""; else { var reg = new RegExp("({[" + i + "]})", "g"); result = result.replace(reg, arguments[i]); return result; else { return this; };
String.prototype.format = function(args) { 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 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 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 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;
function valPhone(phoneNumber) { if (!/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(phoneNumber)) { return false; } else { return true; String.prototype.format = function(args) { if (arguments.length>0) { ...
String.prototype.format = function (args) { var str = this; return str.replace(String.prototype.format.regex, function (item) { var intVal = parseInt(item.substring(1, item.length - 1)); var replace; if (intVal >= 0) { replace = args[intVal]; } else if (intVal === -1) { replace = "{"; ...
'use strict'; String.prototype.format = function (args) { if (arguments.length>0) { var result = this; console.log(this) if (arguments.length ==1 && typeof(args)=="object") { for(var key in args){ var reg = new RegExp ("({"+key+"})","g"); result = result.replace(reg,args[key]); ...