Skip to content

Instantly share code, notes, and snippets.

@OscarGodson
Created September 16, 2011 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OscarGodson/1221286 to your computer and use it in GitHub Desktop.
Save OscarGodson/1221286 to your computer and use it in GitHub Desktop.
Parses a URL and returns a JS obj of key->value pairs with the URL params
var parseUrl = function(url){
var q = {}
, r = new RegExp("([^?=&]+)(=([^&]*))?", "g");
url.replace(r,function($0, $1, $2, $3){ q[$1] = $3; });
return q;
}
/**
* var example = parseUrl('http://google.com?foo=bar&hello=world');
*
* > example.foo
* "bar"
* > example['hello']
* "world"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment