一. 正则获取 url 中的 query 参数
var url = 'name=ooo&age=10';
var reg = /([^&=]+)=?([^&]*)/g;
执行 reg.exec(url)
,控制台输出:
(3) ["name=ooo", "name", "ooo", index: 0, input: "name=ooo&age=10", groups: undefined]
// 再执行一次 reg.exec(url)
(3) ["age=10", "age", "10", index: 9, input: "name=ooo&age=10", groups: undefined]
说明:/([^&=]+)=?([^&]*)/g
字符 | 含义 |
---|---|
^ | 匹配输入的开始, [^&=] 里表示不包括&= |
() | 分组捕获 |
二. 获取 cookie
function getCookie(n) {
var m = document.cookie.match(new RegExp('(^| )' + n + '=([^;]*)(;|$)'));
return !m ? '' : decodeURIComponent(m[2]);
}
var getCookie = function(cookieName) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
if (cookieName == arr[0]) {
return decodeURI(arr[1]);
}
}
return "";
};
参考链接:
正则匹配url中的query参数信息
正则表达式