Skip to content

Instantly share code, notes, and snippets.

@coodoo
Created May 25, 2013 08:06
Show Gist options
  • Save coodoo/5648333 to your computer and use it in GitHub Desktop.
Save coodoo/5648333 to your computer and use it in GitHub Desktop.
input: "$filter=&$top=3&$skip=3&$orderby=firstName&$format=json&$inlinecount=allpages output: Object {$filter: "", $top: "3", $skip: "3", $orderby: "firstName", $format: "json"…} $callback: "callback" $filter: "" $format: "json" $inlinecount: "allpages" optional: enter key name as 2nd parameter to just get that value
$.extend({
getQueryString: function ( urlString, key) {
function parseParams() {
var params = {},
e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = urlString;
while (e = r.exec(q))
params[d(e[1])] = d(e[2]);
return params;
}
if (!this.queryStringParams)
this.queryStringParams = parseParams();
return key ? this.queryStringParams[key] : this.queryStringParams;
}
});
@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