Created
December 8, 2013 13:35
-
-
Save johnnymo87/7857581 to your computer and use it in GitHub Desktop.
Regex for validating URLs
This file contains hidden or 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
| 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