js前台正则表达式获取url参数

function getQueryString(name) {
var winObj = arguments.length > 1 ? arguments[1] : window;
var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
if (typeof (winObj.location) != "object") { winObj = window; }
if (reg.test(winObj.location.href)) return unescape(RegExp.$2.replace(/\+/g, " ")); return "";
}

例如:”https://i.cnblogs.com/EditPosts.aspx?opt=1“

var opt=getQueryString(opt); 

opt结果是:1

猜你喜欢

转载自www.cnblogs.com/guoqingdragon/p/10892492.html