解析查询字符串:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
var daiStr="https://www.baidu.com/s?wd=%E5%90%89%E5%B0%94%E6%8B%9C%E7%99%BB&rsv_spt=1&rsv_iqid=0x8918dabe0005151c&issp=1&f=3&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=ih_0&rsv_sug3=1&rsv_sug1=1&rsv_sug7=001&rsv_sug2=1&rsv_btype=i&rsp=0&rsv_sug9=es_2_1&rsv_sug4=3943&rsv_sug=9#miaodian";
//取出要解析的字符串
var quStr={};
var start=daiStr.indexOf("?");
var end=daiStr.indexOf("#");
//判断有没有字符串
if(start==-1 || end==-1){
quStr;
}
var quchuStr=daiStr.slice(start+1,end);
var quchuArr=quchuStr.split("&");
//console.log(quchuArr);
//分割每一个元素
// for(var i=0;i<quchuArr.length;i++){
// var xinArr=quchuArr[i].split("=");
// quStr[xinArr[0]]=xinArr[1];
// }
// console.log(quStr.valueOf());
quchuArr.forEach(function(item,index,arr){
var xinArr=item.split("=")
quStr[xinArr[0]]=xinArr[1];
return quStr;
})
console.log(quStr.valueOf());
</script>
</body>
</html>
函数解析查询字符串
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>函数解析查询字符串</title>
</head>
<body>
<script>
var daiStr="https://www.baidu.com/s?wd=%E5%90%89%E5%B0%94%E6%8B%9C%E7%99%BB&rsv_spt=1&rsv_iqid=0x8918dabe0005151c&issp=1&f=3&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=ih_0&rsv_sug3=1&rsv_sug1=1&rsv_sug7=001&rsv_sug2=1&rsv_btype=i&rsp=0&rsv_sug9=es_2_1&rsv_sug4=3943&rsv_sug=9#miaodian";
// var jieArr=quchuStr(daiStr);
// console.log(jieArr);
var xinObj=quchuStr(daiStr);
for(var key in xinObj){
console.log(key+":"+xinObj[key])
}
//取出要解析的字符串
function quchuStr(daiStr){
var quStr={};
var start=daiStr.indexOf("?");
var end=daiStr.indexOf("#");
if(start==-1 || end==-1){
return quStr;
}
//分割字符串
var fenStr=daiStr.slice(start+1,end);
var xinArr=fenStr.split("&");
for(var i=0;i<xinArr.length;i++){
var arr=xinArr[i].split("=");
quStr[arr[0]]=arr[1];
}
return quStr;
}
</script>
</body>
</html>