GaoLi's Blog

正则表达式解析查询字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function parseQueryString(f) {
const d = /(([^?&=]+)(?:=([^?&=]*))*)/g;
const a = {};

let b;
let c;
let e;

while (b = d.exec(f)) {
c = b[2];
e = b[3] || '';
a[c] = decodeURIComponent(e);
}

return a;
}

parseQueryString(window.location.search);