Skip to content

Instantly share code, notes, and snippets.

@wheelerlaw
Last active June 28, 2018 17:55
Show Gist options
  • Save wheelerlaw/5c397e532d027a01a897eeb390bfc050 to your computer and use it in GitHub Desktop.
Save wheelerlaw/5c397e532d027a01a897eeb390bfc050 to your computer and use it in GitHub Desktop.
A regex to parse and validate a URL. Language independent.
/
^(?:([^:\/?#]+):\/\/)?
(?:([a-zA-Z0-9\-_.+]+)(?:\:([a-zA-Z0-9\-_.+]+))?@)?
((?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)(?:\.(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?))*\.?)
(?:\:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[0-9]))?
(\/[^?#]*)?
(?:\?([^#]*))?
(?:#(.*))?$
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment