-
Notifications
You must be signed in to change notification settings - Fork 1
/
getParams.js
29 lines (26 loc) · 874 Bytes
/
getParams.js
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
26
27
28
29
function getParams(url) {
const res = {};
const reg1 = /(?:http:\/\/|https:\/\/)?(?:[^#?]+)(\?[^#]+)?(#.+)?/g;
const reg2 = /([^=&]+)=([^=&]+)/g;
try {
const [_, params, hash] = reg1.exec(url);
if (hash) {
res["HASH"] = hash;
}
while (true) {
const [_, key, value] = reg2.exec(params.substring(1));
res[key] = value;
}
} catch {}
return function (name) {
return res[name];
};
}
// getParams("https://baidu.com?name=tencent&age=30#hash"); // { HASH: '#hash', name: 'tencent', age: '30' }
// getParams("https://baidu.com?name=tencent&age=30"); // { name: 'tencent', age: '30' }
// getParams("https://baidu.com"); // {}
// getParams("https://baidu.com#hash"); // { HASH: '#hash' }
const func = getParams("https://baidu.com?name=tencent&age=30#hash");
func("HASH"); // #hash
func("name"); // tencent
func("age"); // 30