1、根据指定字符分割字符串,获取指定字符前后的字符串
function splitStr(string, str) { //根据指定字符分割字符串,获取指定字符前后的字符串
let strObj = {};
let str_before = string.split(str)[0];
let str_after = string.split(str)[1];
strObj.str_before = str_before;
strObj.str_after = str_after;
return strObj;
}
2、解析GET请求url上?后的请求参数,将请求参数从url上取下来,放到对象{}中
parseURL(url) { //解析GET请求url上?后的请求参数,将请求参数从url上取下来,放到{}中
let result = {},
seg = url.split('&'),len = seg.length,i = 0,s;
for (; i < len; i++) {
if (!seg[i]) {
continue;
}
s = seg[i].split('=');
result[s[0]] = decodeURIComponent(s[1]);
}
return result;
},
试一试测试上面的两个方法,修改成适合自己的方法:菜鸟教程在线编辑器
let str='/api/getArticles?orgid=abc&type=新闻&page=1&limit=10';
let strObj=splitStr(str,'?')
let before=strObj.str_before;
let after=strObj.str_after;
console.log(before);// /api/getArticles
console.log(after);// orgid=abc&type=新闻&page=1&limit=10
let data=parseURL(after);
console.log(data);// {orgid: "abc", type: "新闻", page: "1", limit: "10"}