JS中String类型添加占位符功能
//String类型添加占位符功能
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;
}
}
本文标题:JS中String类型添加占位符功能
本文链接:https://www.haomeiwen.com/subject/tmkmnctx.html
网友评论