JavaScript.format
http://www.xggxgg.com/685.html
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; }
使用方法: 一,字面量版:"为什么{language}没有format".format ( { language : "javascript" } ); 二,数组版:"为什么{0}没有format".format ( [ "javascript" ] );
(function($){ $.extend({ format : function(source,args){ var result = source; if(typeof(args) == "object"){ if(args.length==undefined){ 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 < args.length; i++) { if (args[i] != undefined) { var reg = new RegExp("({[" + i + "]})", "g"); result = result.replace(reg, args[i]); } } } } return result; } }) })(jQuery);
使用方法: 一,字面量版:$.format ( "为什么{language}没有format" , { language : "javascript" } ); 二,数组版:$.format ( "为什么{0}没有format" , [ "javascript" ] );
相关推荐
nmgxzm00 2020-11-10
ifconfig 2020-10-14
hhanbj 2020-11-17
zfszhangyuan 2020-11-16
古叶峰 2020-11-16
一个智障 2020-11-15
jipengx 2020-11-12
81427005 2020-11-11
xixixi 2020-11-11
游走的豚鼠君 2020-11-10
苗疆三刀的随手记 2020-11-10
Web卓不凡 2020-11-03
小飞侠V 2020-11-02
帕尼尼 2020-10-30
爱读书的旅行者 2020-10-26
帕尼尼 2020-10-23
杏仁技术站 2020-10-23
淼寒儿 2020-10-22