js正則獲取url所帶參數值


在js字符串對象原型中添加這個獲取鏈接參數值方法,getAddrVal():

String.prototype.getAddrVal = String.prototype.getAddrVal||function(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var data = this.substr(1).match(reg);
    return data!=null?decodeURIComponent(data[2]):null;
}

調用:

var s = window.location.search; //先截取當前url中“?”及后面的字符串

s.getAddrVal(''); //調用本方法,以字符串形式傳入要獲取的參數名,如s.getAddrVal('keyId');

打賞

免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2023 CODEPRJ.COM