JavaScript string.format

//string.format
        String.prototype.format=function(){
            var e = this, f = arguments.length;    
            if (f > 0) {        
                for ( var d = 0; d < f; d++) {            
                    e = e.replace(new RegExp("\{" + d + "\}", "g"), arguments[d])        
                }    
            }    
            return e
        };

JavaScript 实现字符串占位。

String.prototype.format =function(){
var e =this, f = arguments.length;
if(f >0){
for(var d =0; d < f; d++){
e = e.replace(newRegExp("\{"+ d +"\}","g"), arguments[d])
}
}
return e
};
 
var msg ="我爱我的家人{0},我的家人也爱我{1}";
var a = msg.format("xixi...","haha...");
alert(a);//我爱我的家人 xixi... ,我的家人也爱我haha...
 
原文地址:https://www.cnblogs.com/qixue/p/3549582.html