0

I need to validate URL with URI::regexp but some URLs have no scheme.

I tried using,

URI::regexp(["http", "http]) 
URI::regexp(%w(http https tel)

but they cannot validate empty scheme such as "www.google.com" "google.com".

How can I include/validate such URLs?

Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36

2 Answers2

0

Try URI::DEFAULT_PARSER.regexp[:ABS_URI] from the source itself. It also fails in specific conditions as mentioned in "How to check if a URL is valid", but if you are ok with it, it's good to go:

def valid?(url)
  !!(url =~ URI::ABS_URI || url =~ URI::REL_URI)
end
Michael Koper
  • 9,586
  • 7
  • 45
  • 59
Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36
0
inp = %w|www.google.com
         www.http.com
         http://google.com
         https://google.com|.map do |e|
  e.gsub(/\A(?!http)/, "http://")
end
#⇒ [
#   [0] "http://www.google.com",
#   [1] "http://www.http.com",
#   [2] "http://google.com",
#   [3] "https://google.com"
# ]
_.map { |e| URI::regexp(%w|http https|) =~ e }
#⇒ [0, 0, 0, 0]
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160