Skip to content

Instantly share code, notes, and snippets.

@jhuckaby
Last active September 26, 2015 16:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhuckaby/1125790 to your computer and use it in GitHub Desktop.
Save jhuckaby/1125790 to your computer and use it in GitHub Desktop.
Parse query string into key/value pairs and return as object
function parseQueryString(url) {
// parse query string into key/value pairs and return as object
var query = {};
url = (url || location.search).replace(/^.*\?/, '').replace(/([^\=]+)\=([^\&]*)\&?/g, function(match, key, value) {
query[key] = decodeURIComponent(value);
return '';
} );
return query;
}
@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