Skip to content

Instantly share code, notes, and snippets.

@danlzh
Created March 17, 2017 08:59
Show Gist options
  • Save danlzh/e07b4ad8b285fc3e73279b561b2fb9d5 to your computer and use it in GitHub Desktop.
Save danlzh/e07b4ad8b285fc3e73279b561b2fb9d5 to your computer and use it in GitHub Desktop.
get url parameter regex in browser
function getURLParam(name, url){
url = url || location.search;
url.startsWith("?") || (url="?"+url);
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
getURLParam("a", "a=AAA&b=BBB&c=CCC");
//AAA
getURLParam("ba", "a=AAA&b=BBB&c=CCC");
//null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment