使用的string.format替换字符串

String.format = function(src){
    if (arguments.length == 0) return null;
    var arr = Array.prototype.slice.call(arguments, 1);
    var args=arr.toString().split(',');
    return src.replace(/\{(\d+)\}/g, function(m, i){
       
//        var isdate=String.IsDateStrAndNum(args[i]);
//        if(isdate){
//            return args[i];
//        }
//        if(args[i].length>5){
//            return args[i].substr(0,5)+"..";
//        }
        return args[i];
    });
};
String.IsDateStrAndNum=function(dateStr){
    var regexp=/^\d{4}-\d{2}-\d{2}\s{1}\d{2}:\d{2}:\d{2}$/;
    var regexp2=/^\d{0,20}$/;
    return regexp.test(dateStr) || regexp2.test(dateStr);

};

/***********************************************************/

row="this is a {0} for String.format {1} in the string can contains {} is also can replace create";

//方法一

var arr = new Array("demo","function");

row=String.format(row,arr);

alert(row);//this is a demo for String.format function in the string can contains {} is also can replace create

//方法二

row=String.format(row,"demo","function");

alert(row);//this is a demo for String.format function in the string can contains {} is also can replace create

 

 

 

 

 

你可能感兴趣的:(使用的string.format替换字符串)