Javascript 笔试题总结

给定一个URL字符串,要求用js实现一个函数,找出其中的query值
例如:URL:http://www.qunar.com/plane/queryPlane.html?startTime=xxxx&endTime=xxxxx#tags

1
2
3
4
5
6
7
8
9
10
11
12
function getQueryParam(url, name) {
var a = document.createElement('a');
a.href = url;
var query = a.search;
var len = query.length;
var reg = /[?&]([^\d]\w+)=(\w+)/gi;
var res = reg.exec(query);
while (reg.lastIndex < len && res[1] && res[1] !== name) {
res = reg.exec(query);
}
return res[2];
}