//第一种方式
const url = 'http://baidu.com/?a=1&b=2'
const parseQuery = (url) => {
  q = {}
  url.replace(/([^?&=])=([^&]+)/g, (_, k, v) => (q[k] = v))
  return q
}
const text = parseQuery(url)
console.log(text);

//第二种方式
const urlParams = new URLSearchParams(new URL(url).search);
const paramsObj = {};
for (let [key, value] of urlParams) {
  paramsObj[key] = value;
}
console.log(paramsObj);
Last Updated:
Contributors: pengrengui