1、将所有参数转化为对象,重复的参数作为数组:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
function parseQuery(url) {
var queryObj={};
var reg=/[?&]([^=&
#]+)=([^&#]*)/g;
var querys=url.match(reg);
if(querys){
for(
var i
in querys){
var query=querys[i].split(
'=');
var key=query[
0].substr(
1),
value=query[
1];
queryObj[key]?queryObj[key]=[].concat(queryObj[key],136);
Box-sizing: border-
Box;">value):queryObj[key]=
value;
}
}
return queryObj;
}
var url=
'http:www.baidu.com/index?name=username&age=27&pwd=zbc|123@&likes=lol&likes=beautifull girl&$id=main#flag=66';
console.log(parseQuery(url));
{ name:
'username',age:
'27',pwd:
'zbc|123@',likes: [
'lol',
'beautifull girl' ],0);
Box-sizing: border-
Box;">'$id':
'main'
}
2、获取某个url中某个参数的值
1
2
3
4
5
6
7
8
9
10
11
12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
function getQueryByName(url,name){
var reg=
new RegExp(
'[?&]'+name+
'=([^&#]+)');
var query=url.match(reg);
return query?query[
1]:
null;
}
'http:www.baidu.com/index?name=wangqingqiang&age=27&pwd=wqq123@#flag=66';
var value=getQueryByName(url,0);
Box-sizing: border-
Box;">'pwd');
console.log(value);
wqq123@