1. 解析URL对象
字符串拆分,使用indexOf,split
function queryUrl(URL) {
let obj = {};
if (URL.indexOf('?') < 0) return obj;
let arg = URL.split("?")[1].split("&");
arg.forEach(val => {
let arr = val.split('=');
obj[arr[0]] = arr[1];
});
return obj;
}
正则匹配
function queryUrl(URL) {
let reg = /([^&?=]+)=([^&?=]+)/g
let obj = {};
URL.replace(reg,(...arg)=>{
obj[arg[1]]= arg[2]
})
return obj;
}
字符串优先使用正则处理
2. 闭包