文章目录

比较常用的url解析的代码,正则表达式检索实现匹配参数,(边学习边总结) 例如:解析url中的参数———–?id=12345&a=b 返回值:Object {id:12345, a:b}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function urlParse() {
let url = window.location.search;
let obj = {};
let reg = /[?&][^?&]+=[^?&]+/g;
let arr = url.match(reg);
// ['?id=12345', '&a=b']
if (arr) {
arr.forEach((item) => {
let temArr = item.substring(1).split('=');
let key = decodeURIComponent(temArr[0]);
let value = decodeURIComponent(temArr[1]);
obj[key] = value;
});
}
return obj;
};

来源: https://blog.csdn.net/weixin_38483133/article/details/85200619

文章目录