Created
September 16, 2011 05:42
-
-
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
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
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