# 解析URL参数到键值Object > 纯es6写法, 目前很多老浏览器不兼容(2019). 不建议在web使用 建议用在uniapp之类上 ~~~ 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 tempArr = item.substring(1).split('='); let key = decodeURIComponent(tempArr[0]); let val = decodeURIComponent(tempArr[1]); obj[key] = val; }); } return obj; }; ~~~ # 测试 ``` console.log(urlParse("http://edk24.com?id=66&type=bargain")); ------------------ {id: "66", type: "bargain"} ``` # End 来源:[https://zhuanlan.zhihu.com/p/25064442](https://zhuanlan.zhihu.com/p/25064442)