字符串格式化---- String.prototype.format
1 String.prototype.format = function (args) { 2 return this.replace(/\{(\w+)\}/g, function (g0, g1) { # g0,g1表示函数前的参数字符串的索引 3 return args[i]; 4 }); 5 };
例2:
1 //扩展 string.format 2 String.prototype.format = function () { 3 var args = arguments; 4 var reg = /\{(\d+)\}/g; 5 return this.replace(reg, function (g0, g1) { 6 return args[+g1]; 7 }); 8 }; 9 //用法: 10 var k="a{0}b{1}".format("qqq","www"); 11 12