Skip to content

Instantly share code, notes, and snippets.

@mitio
Created March 29, 2011 14:09
Show Gist options
  • Save mitio/892421 to your computer and use it in GitHub Desktop.
Save mitio/892421 to your computer and use it in GitHub Desktop.
A simple function to return a hash with parameters, parsed from a query string.
// Returns a hash with the query parameters, parsed from the query string given
// (or the current location's query string, if no arguments given)
// Based on code found here:
// http://stackoverflow.com/questions/901115/get-querystring-values-in-javascript/2880929#2880929
function getQueryParams(queryString) {
var queryParams = {},
e,
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(/\+/g, ' ')); },
q = queryString || window.location.search.substring(1);
while (e = r.exec(q)) {
queryParams[d(e[1])] = d(e[2]);
}
return queryParams;
}
@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