Skip to content

Instantly share code, notes, and snippets.

@itsdouges
Created September 4, 2017 05:59
Show Gist options
  • Save itsdouges/33cab23455b13091d86ee1860ef6ac31 to your computer and use it in GitHub Desktop.
Save itsdouges/33cab23455b13091d86ee1860ef6ac31 to your computer and use it in GitHub Desktop.
Very naive query string implementation
// @flow
export default function getParameterByName (
name: string,
url: string = window.location.href
): string {
const normalisedName = name.replace(/[[\]]/g, '\\$&');
const regex = new RegExp(`[?&]${normalisedName}(=([^&#]*)|&|#|$)`);
const results = regex.exec(url);
if (!results || !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