1. 程式人生 > >js中的string.format函式程式碼

js中的string.format函式程式碼

<1>

  1. String.prototype.format = function(args) {

  2. var result = this;

  3. if (arguments.length > 0) {

  4. if (arguments.length == 1 && typeof (args) == "object") {

  5. for (var key in args) {

  6. if(args[key]!=undefined){

  7. var reg = new RegExp("({" + key + "})", "g");

  8. result = result.replace(reg, args[key]);

  9. }

  10. }

  11. }

  12. else {

  13. for (var i = 0; i < arguments.length; i++) {

  14. if (arguments[i] != undefined) {

  15. //var reg = new RegExp("({[" + i + "]})", "g");//這個在索引大於9時會有問題

  16. var reg = new RegExp("({)" + i + "(})", "g");

  17. result = result.replace(reg, arguments[i]);

  18. }

  19. }

  20. }

  21. }

  22. return result;

  23. }

呼叫方式:

  1. //兩種呼叫方式

  2. var template1="我是{0},今年{1}了";

  3. var template2="我是{name},今年{age}了";

  4. var result1=template1.format("loogn",22);

  5. var result2=template2.format({name:"loogn",age:22});

  6. //兩個結果都是"我是loogn,今年22了"

方案2:方法呼叫

  1. function stringFormat() {

  2. if (arguments.length == 0)

  3. return null;

  4. var str = arguments[0];

  5. for (var i = 1; i < arguments.length; i++) {

  6. var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');

  7. str = str.replace(re, arguments[i]);

  8. }

  9. return str;

  10. }

呼叫方式:

StringFormat("&Type={0}&Ro={1}&lPlan={2}&Plan={3}&={4}&Id={5}&Id={6}", data1, data2, data3,data4, data5,data6,data7);


方案3:
相信做前端開發的朋友都受過這個折磨:連線HTML的時候被可惡的單引號、雙引號搞得頭昏腦脹。比如:
element.innerHTML = ‘<a href=”‘ + url + ‘” onclick=”alert(\” + msg + ‘\’);”>’ + text + ‘</a>’;
這裡介紹一個字串格式化函式:

  1. String.format = function(str) {

  2. var args = arguments, re = new RegExp(”%([1-" + args.length + "])”, “g”);

  3. return String(str).replace(

  4. re,

  5. function($1, $2) {

  6. return args[$2];

  7. }

  8. );

  9. };

呼叫方式:

element.innerHTML = String.format(’<a href=”%1″ onclick=”alert(\’%2\’);”>%3</a>’, url, msg, text);

<3>

String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function(m, i){ return args[i]; }); }

//V2 static String.format = function() { if( arguments.length == 0 ) return null; var str = arguments[0];

for(var i=1;i<arguments.length;i++) { var re = new RegExp('\\{' + (i-1) + '\\}','gm');

str = str.replace(re, arguments[i]); } return str; }

var a = "I Love {0}, and You Love {1},Where are {0}! {4}";

alert(String.format(a, "You","Me"));

alert(a.format("You","Me"));

<2>

String.prototype.format = function(args)

{

if (arguments.length > 0)

{

var result = 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]);

}

}

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;

}

}

  //兩種呼叫方式 
var template1="我是{0},今年{1}了"; 
var template2="我是{name},今年{age}了"; 
var result1=template1.format("loogn",22); 
var result2=template1.format({name:"loogn",age:22}); 
//兩個結果都是"我是loogn,今年22了" 

<3>