我需要使用URI :: regexp验证URL,但有些URL没有方案。
我尝试过使用,
URI::regexp(["http", "http])
URI::regexp(%w(http https tel)
但他们无法验证空白方案,例如“www.google.com”“google.com”。
如何包含/验证此类网址?
答案 0 :(得分:0)
从source本身尝试URI::DEFAULT_PARSER.regexp[:ABS_URI]
。它也在" How to check if a URL is valid"中提到的特定条件下失败,但是如果你对它好的话,那就不错了:
def valid?(url)
!!(url =~ URI::ABS_URI || url =~ URI::REL_URI)
end
答案 1 :(得分: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]