# 取URL查询参数为对象 <br /> ## 方法一 ```JavaScript function getQueryObject(url) { url = url == null ? window.location.href : url const search = url.substring(url.lastIndexOf('?') + 1) const obj = {} const reg = /([^?&=]+)=([^?&=]*)/g search.replace(reg, (rs, $1, $2) => { const name = decodeURIComponent($1) let val = decodeURIComponent($2) val = String(val) obj[name] = val return rs }) return obj } getQueryObject('http://abc.com?name=uzi&mode=1') ------------- { name: 'uzi', mode: '1' } ``` <br /><br /> ## 方法二 ```JavaScript function param2Obj(url) { const search = url.split('?')[1] if (!search) { return {} } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') .replace(/\+/g, ' ') + '"}' ) } param2Obj('http://abc.com?name=uzi&mode=1') ------------- { name: 'uzi', mode: '1' } ```