Skip to content

Instantly share code, notes, and snippets.

@b-bot
Created July 25, 2019 08:16
Show Gist options
  • Save b-bot/f16d091f5082b4e325be4b2c7a049d9d to your computer and use it in GitHub Desktop.
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`.
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