Skip to content

Instantly share code, notes, and snippets.

@johnnymo87
Created December 8, 2013 13:35
Show Gist options
  • Select an option

  • Save johnnymo87/7857581 to your computer and use it in GitHub Desktop.

Select an option

Save johnnymo87/7857581 to your computer and use it in GitHub Desktop.
Regex for validating URLs
From top comment on: http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/
// (?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
//
// $@ matches the entire url
// $1 matches scheme (http, ftp, mailto, ym, mshelp, etc)
// $2 matches authority (host, ftp user:pwd@host, etc)
// $3 matches path
// $4 matches query (http GET REST api, etc)
// $5 matches fragment (html anchor, etc)
//
// Match specific schemes, non-optional authority, disallow white-space so can delimit in text, and allow 'www.' w/o scheme
// Note the schemes must match ^[^|:/?#\s]+(?:\|[^|:/?#\s]+)*$
//
// (?:()(www\.[^/?#\s]+\.[^/?#\s]+)|(schemes)://([^/?#\s]*))([^?#\s]*)(\?([^#\s]*))?(#([^\s]*))?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment