format函数

来源:互联网 发布:网络推广付费平台 编辑:程序博客网 时间:2024/05/17 06:34
format函数实现自动替换字符串中占位变量,避免使用加号连接字符串。
<script type="text/javascript">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}/{2}".format("www.meizz.com", "web", "abc.htm"));alert("请输入{0},输完后再按存盘按钮".format("姓名"));</script>