URI.parse为无效的URL返回0

时间:2015-05-15 14:45:57

标签: ruby

我想要验证的无效网址无效,但Ruby的URI说它有效:

url = "https://blah.%20wordpress.com/"
# => "https://blah.%20wordpress.com/"
url =~ URI::DEFAULT_PARSER.regexp[:ABS_URI]
# => 0

我有办法确认它无效吗?

1 个答案:

答案 0 :(得分:2)

尝试URI::DEFAULT_PARSER.regexp[:UNSAFE]

url = "https://blah.%20wordpress.com/"
url =~ URI::DEFAULT_PARSER.regexp[:UNSAFE]
# => 13

safe_url = "https://blah.wordpress.com/"
safe_url =~ URI::DEFAULT_PARSER.regexp[:UNSAFE]
# => nil