Skip to content

Instantly share code, notes, and snippets.

@corazzi
Forked from kares/jquery.parseparams.js
Last active August 14, 2019 09:26
Show Gist options
  • Save corazzi/c181e451f1872846d9ca5269018a909e to your computer and use it in GitHub Desktop.
Save corazzi/c181e451f1872846d9ca5269018a909e to your computer and use it in GitHub Desktop.
parseQueryParams - parse query string parameters into an object [non-jQuery version]
parseQueryParams = function (query) {
var re = /([^&=]+)=?([^&]*)/g;
var decodeRE = /\+/g;
var decode = function (str) {
return decodeURIComponent(str.replace(decodeRE, " "));
};
var params = {}, e;
while (e = re.exec(query)) {
var k = decode(e[1]), v = decode(e[2]);
if (k.substring(k.length - 2) === '[]') {
k = k.substring(0, k.length - 2);
(params[k] || (params[k] = [])).push(v);
} else params[k] = v;
}
return params;
};
@corazzi
Copy link
Author

corazzi commented Aug 14, 2019

Works with grouped[]=values, too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment