Skip to content

Instantly share code, notes, and snippets.

@beatak
Created May 31, 2013 19:08
Show Gist options
  • Save beatak/5687204 to your computer and use it in GitHub Desktop.
Save beatak/5687204 to your computer and use it in GitHub Desktop.
Works in progress and will make a github repo for this for realz. currently I copied from http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values things i wanted it be able to parse are: - array -- ?a[]=1&a[]=2 // => { a: [1, 2]} -- ?a[0]=1&a[1]=2 // => { a: [1, 2] } **maybe?** - different separator. i think ; (semi-colon) is…
var parseQueryString = ( function () {
"use strict";
return function (query) {
var match,
urlParams = {},
pl = /\+/g,
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); };
query = query || window.location.search;
if ( 0 === query.indexOf('?')) {
query = query.substring(1);
}
while (match = search.exec(query)) {
urlParams[decode(match[1])] = decode(match[2]);
}
return urlParams;
};
})();
@sindresorhus
Copy link

Using regex for this is just wrong and will fail in some scenarios.

You'd better off using a tiny reusable module like:
https://github.com/sindresorhus/query-string

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