url 获取参数4中方法
1 正则表达式
let url =
"https://blog.csdn.net/qq_43290288/article/details/106683304?aa=22&&cc=33&&spm=1001.2101.3001.6661.1";
let reg = /(\w+)=(\w+)/g;
let obj = {};
while (reg.exec(url)) {
if (obj[RegExp.$1]) {
obj[RegExp.$1] = [].concat(obj[RegExp.$1], obj[RegExp.$2]);
} else {
obj[RegExp.$1] = RegExp.$2;
}
}
2 url 切割 split
let url =
"https://blog.csdn.net/qq_43290288/article/details/106683304?aa=22&&cc=33&&spm=1001.2101.3001.6661.1";
let str = url.split("?")[1].split("&&");
let obj = {};
str.forEach((item) => {
let temp = item.split("=");
obj[temp[0]] = temp[1];
});
3 qs的parse方法
import { parse } from "qs";
let url =
"https://blog.csdn.net/qq_43290288/article/details/106683304?aa=22&&cc=33&&spm=1001.2101.3001.6661.1";
let str = url.split("?")[1];
console.log(parse(str));
4 正则表达式 replace
let url =
"https://blog.csdn.net/qq_43290288/article/details/106683304?aa=22&&cc=33&&spm=1001.2101.3001.6661.1";
let obj = {};
url.split("?")[1].replace(/(\w+)=(\w+)/g, function($0, $1, $2) {
console.log($0, $1, $2);
if (obj[$1]) {
obj[$1] = [].concat(obj[$1], $2);
} else {
obj[$1] = $2;
}
});