url 解析
实现一个方法,拆解URL参数中queryString
const url = 'http://sample.com/?a=1&b=2&c=xx&d=2#hash';
const result = { a: '1', b: '2', c: 'xx', d: 2 };
常规解法
const dismantle = (url) => {
const aimUrl = url.split('?').pop().split('#').shift().split('&');
const res = {};
aimUrl.forEach(item => {
const [key, val] = item.split('=');
res[key] = val;
});
return res;
}
正则快速解法
const getUrlParams = (url)=>{
const obj = {}
url.replace(/([^?&]+)=([^&#]+)/g, (_, k, v) => {
console.log(_,k,v)
obj[k] = v
})
return obj
}