Created
July 25, 2019 08:16
-
-
Save b-bot/f16d091f5082b4e325be4b2c7a049d9d to your computer and use it in GitHub Desktop.
REGEX-based solution to getting any parameter in a given URL by passing `name`.
This file contains 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
getParameterByName(name: string, url: string) { | |
if (!url) { url = window.location.href; } | |
name = name.replace(/[\[\]]/g, '\\$&'); | |
const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), | |
results = regex.exec(url); | |
if (!results) { return null; } | |
if (!results[2]) { return ''; } | |
return decodeURIComponent(results[2].replace(/\+/g, ' ')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment