天天看点

JS添加类似C# string.Format方法

String.prototype.format=function()  

{  

  if(arguments.length==0) return this;  

  for(var s=this, i=0; i<arguments.length; i++)  

    s=s.replace(new RegExp("\\{"+i+"\\}","g"), arguments[i]);  

  return s;  

};  

//占位符可以为多个  

alert("http://{0}/{1}".format("www.songyanjun.net", "index.html"));  

//与上同理  

alert("请输入{0},输完后再按存盘按钮".format("姓名"));