Skip to content

Instantly share code, notes, and snippets.

@triskweline
Last active September 1, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triskweline/dd76aee4713cd5b8d66e to your computer and use it in GitHub Desktop.
Save triskweline/dd76aee4713cd5b8d66e to your computer and use it in GitHub Desktop.
Regular expression to sanity check URLs
URL = %r{
\A # Beginning of string
(http|https) # $1: Scheme
:\/\/ # ://
([\w\-_]+(?:\.[\w\-_]+)*) # $2: Hostname. Does not begin or end with a dot. No two dots in a row.
(?::(\d+)) # $3: Port (without leading colon)
(/[^/\?\#]*) # $4: Path (with leading slash)
(?:\?([^\#]*)) # $4: Query (without leading question mark)
(?:\#(.*)) # $5: Hash (without leading hash)
\z # End of string
}x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment