写一个函数,使用正则获取当前浏览器的多参数
如 http://www.sodacar.com/vehicles?car=13242&station=83212jde
返回 {car: "13242", station: "83212jde"}
实现:
// 方案一
const parseQueryString = (url) => {
const reg_url = /^[^\?]+\?([\w\W]+)$/
const reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g
const arr_url = reg_url.exec(url)
let ret = {}
if (arr_url && arr_url[1]) {
let str_para = arr_url[1]
let = result
while ((result = reg_para.exec(str_para)) != null) {
// debugger
ret[result[1]] = result[2]
}
}
return ret
}
// 方案二
const getParams = (url) => {
let obj = {}
const par = /^[^\?]+\?([\w\W]+)$/.exec(url)[1]
par.replace(/([^&=]+)=([\w\W]*?)(&|$|#)/g, (match, $1, $2) => {
obj[$1] = $2
})
return obj
}
网友评论