Created
March 17, 2017 08:59
-
-
Save danlzh/e07b4ad8b285fc3e73279b561b2fb9d5 to your computer and use it in GitHub Desktop.
get url parameter regex in browser
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
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