const url = "http://www.baidu.com/m?f=8&ie=utf-8&rsv_bp=1&tn=monline_3_dg&wd=session"
const queryURLparamsRegEs5 = (url)=>{
let obj = {};
let reg = /([^?=&]+)=([^?=&]+)/g;
url.replace(reg, (...args)=>{
obj[args[1]] = args[2];
})
return obj
}
// let reg = /([^?=&]+)=([^?=&])+)/g;
// url.replace(reg, (...args)=>{
// obj[args[1]] = args[2]
// })
//方法二: 正则 + arguments
//正则匹配规则 /([^?=&]+)=([^?=&]+)/g
//利用 replace 替换
//用伪数组进行键值对拼接
// const queryURLparamsRegEs5 = (url)=>{
// let obj = {}
// let reg = /([^?=&]+)=([^?=&]+)/g;
// url.replace(reg,function(){
// //在这个代码中,使用箭头函数作为 replace() 方法的回调函数会导致错误,是因为箭头函数没有自己的 arguments 对象。
// //在箭头函数中,arguments 并不是指向函数的参数,而是指向包含箭头函数的父级函数的 arguments 对象。因此,在箭头函数中使用 arguments[1] 和 arguments[2] 会引发错误,因为父级函数中并没有这些参数。
// console.log(arguments)
// obj[arguments[1]] = arguments[2]
// })
// return obj
// }
//方法三: 正则 + ..arg
//就是用 ES6 的 …arg
//其实和 arguments 差不多 ,就是 arguments 是伪数组,…arg 是真数组
// const queryURLparamsRegEs5 = (url)=>{
// let obj = {}
// let reg = /([^?=&]+)=([^?=&]+)/g;
// url.replace(reg,(...args)=>{
// console.log(args)
// obj[args[1]] = args[2]
// })
// return obj
// }
console.log(queryURLparamsRegEs5(url))
前端正则解析url
最新推荐文章于 2024-07-18 17:32:29 发布