Last active
September 26, 2015 16:28
-
-
Save jhuckaby/1125790 to your computer and use it in GitHub Desktop.
Parse query string into key/value pairs and return as object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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