码迷,mamicode.com
首页 > Web开发 > 详细

js常用方法收集

时间:2015-05-23 11:20:09      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

JS获取地址栏制定参数值:

//获取URL参数的值
function   getUrlParam(name){      
          var   reg   =   new   RegExp("(^|&)"+   name   +"=([^&]*)(&|$)");      
          var   r   =   window.location.search.substr(1).match(reg);      
          if   (r!=null)   return   unescape(r[2]);   return   null;      
      }  

JS去除空字符:

String.prototype.trim=function() 
{    
return this.replace(/(^\s*)|(\s*$)/g,‘‘);
}
var str="   test   ";
alert("["+str+"]"); // [   test  ]
alert("["+str.trim()+"]"); // [test]

JS字符串格式化:

/** 格式化输入字符串**/
//用法: "hello{0}".format(‘world‘);返回‘hello world‘
     String.prototype.format= function(){
       var args = arguments;
       return this.replace(/\{(\d+)\}/g,function(s,i){
         return args[i];
       });
}

var str = "hello{0}{1}".format(‘world‘,‘haha‘);
alert(str);

  

js常用方法收集

标签:

原文地址:http://www.cnblogs.com/loyung/p/4523728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!