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