Created
August 30, 2014 17:40
-
-
Save chrisveness/0e1f8174a3b41f2ace3d to your computer and use it in GitHub Desktop.
Get query string argument (using regexp)
This file contains hidden or 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
/** | |
* Returns specified argument from query string. | |
* | |
* @params {string} key - Argument to be returned. | |
* @returns {string} Value of key ('' for ?arg=, null for ?arg, undefined if not present). | |
*/ | |
function getQueryArg(key) { | |
// look for key prefixed by ?/&/;, (optionally) suffixed | |
// by =val (using lazy match), followed by &/;/# or EOS | |
var re = new RegExp('[?&;]'+key+'(=(.*?))?([&;#]|$)'); | |
var results = re.exec(location.search); | |
if (results == null) return undefined; // not found | |
if (results[2] == undefined) return null; // ?key without '=' | |
return decodeURIComponent(results[2].replace(/\+/g, ' ')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment