Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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(;
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
You can’t perform that action at this time.