Check if given string is an URL Check if given string is an URL ruby ruby

Check if given string is an URL


Use the URI library.

def uri?(string)  uri = URI.parse(string)  %w( http https ).include?(uri.scheme)rescue URI::BadURIError  falserescue URI::InvalidURIError  falseend

This is a very simple example. The advantage of using the URI library over a regular expression is that you can perform complex checks.


validates :url, :format => URI::regexp(%w(http https))