@
iugo 草草写了个,你感受下
var parseQuery = function(string) {
var urlSplit = string.split('?'), querySplit = [], queries = {};
if(urlSplit.length < 2 || !urlSplit[1])
return null;
querySplit = urlSplit[1].split('&');
for (var i = 0; i < querySplit.length; i++) {
var query = querySplit[i].split('=');
if(query.length > 0 && query[0])
queries[query[0]] = query[1];
};
return queries;
}
console.log(parseQuery('
https://www.baidu.com/s?ie=UTF-8&wd=%E6%B5%8B%E8%AF%95'));console.log(parseQuery('
https://www.baidu.com/s?ie=UTF-8&=%E6%B5%8B%E8%AF%95'));console.log(parseQuery('
https://www.baidu.com/s?ie=UTF-8&wd='));console.log(parseQuery('
https://www.baidu.com/s?ie=UTF-8&='));console.log(parseQuery('
https://www.baidu.com/s?ie=UTF-8'));console.log(parseQuery('
https://www.baidu.com/s?'));console.log(parseQuery('
https://www.baidu.com/s'));测试结果:
{ ie: 'UTF-8', wd: '%E6%B5%8B%E8%AF%95' }
{ ie: 'UTF-8' }
{ ie: 'UTF-8', wd: '' }
{ ie: 'UTF-8' }
{ ie: 'UTF-8' }
null
null