Skip to content

Instantly share code, notes, and snippets.

@chrisveness
Created August 30, 2014 17:40
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 chrisveness/0e1f8174a3b41f2ace3d to your computer and use it in GitHub Desktop.
Save chrisveness/0e1f8174a3b41f2ace3d to your computer and use it in GitHub Desktop.
Get query string argument (using regexp)
/**
* 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