Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Last active February 4, 2021 10:16
Show Gist options
  • Save keyvanakbary/4972366 to your computer and use it in GitHub Desktop.
Save keyvanakbary/4972366 to your computer and use it in GitHub Desktop.
Http URL regex based on RFC 1738
class HttpUrl
ALPHA = "[a-zA-Z]"
DIGIT = "[0-9]"
DIGITS = "#{DIGIT}+"
SAFE = "[-$_.+]"
EXTRA = "[!*'(),]"
ALPHADIGIT = "[a-zA-Z0-9]"
TOPLABEL = "#{ALPHA}(#{ALPHADIGIT}|-)*#{ALPHADIGIT}|#{ALPHA}"
DOMAINLABEL = "#{ALPHADIGIT}(#{ALPHADIGIT}|-)*#{ALPHADIGIT}|#{ALPHADIGIT}"
HOSTNAME = "((#{DOMAINLABEL})\\.)*#{TOPLABEL}"
HOSTNUMBER = "#{DIGITS}\\.#{DIGITS}\\.#{DIGITS}\\.#{DIGITS}"
HOST = "#{HOSTNAME}|#{HOSTNUMBER}"
HOSTPORT = "(#{HOST})(:#{DIGITS})?"
HEX = "#{DIGIT}|A-F|a-f"
ESCAPE = "%#{HEX}#{HEX}"
UNRESERVED = "#{ALPHA}|#{DIGIT}|#{SAFE}|#{EXTRA}"
UCHAR = "#{UNRESERVED}|#{ESCAPE}"
HSEGMENT = "(#{UCHAR}|;|\\?|&|=)*"
SEARCH = "(#{UCHAR}|;|\\?|&|=)*"
HPATH = "#{HSEGMENT}(\/#{HSEGMENT})*"
HTTPURL = "(http[s]?:\/\/#{HOSTPORT}(\/#{HPATH}(\\?#{SEARCH})?)?)"
@buildRegExp: ->
new RegExp(HTTPURL, "g")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment