2017-06-06 73 views
0

我需要使用URI :: regexp驗證URL,但有些URL沒有方案。Rails的URI :: regexp匹配沒有計劃

我嘗試使用,

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

,但他們無法驗證空計劃,如 「www.google.com」, 「google.com」。

如何包含/驗證此類網址?

回答

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 
+0

這是一個有效的URL here:在有效的(「富」) –

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]