module UrlRegex

Provides the best known regex for validating and extracting URLs. It uses amazing job done by [Diego Perini](gist.github.com/dperini/729294) and [Mathias Bynens](mathiasbynens.be/demo/url-regex).

Constants

BASE
JAVASCRIPT_BASE
MODES
PROTOCOL_IDENTIFIER
PROTOCOL_IDENTIFIER_OPTIONAL
VERSION

Public Class Methods

get(scheme_required: true, mode: :validation) click to toggle source

Returns the regex for URLs parsing or validating.

@param scheme_required [Boolean] will the regex require scheme presence, defaults to true @param mode [Symbol] purpose of the regex, `:validation` or `parsing`, defaults to `:validation` @return [Regex] regex for parsing or validating

# File lib/url_regex.rb, line 12
def self.get(scheme_required: true, mode: :validation)
  raise ArgumentError, "wrong mode: #{mode}" if MODES.index(mode).nil?

  scheme = scheme_required ? PROTOCOL_IDENTIFIER : PROTOCOL_IDENTIFIER_OPTIONAL

  case mode
  when :validation
    /\A#{scheme} #{BASE}\z/xi
  when :parsing
    /#{scheme} #{BASE}/xi
  when :javascript
    /^#{scheme}#{JAVASCRIPT_BASE}$/
  end
end