Reputation: 81
Could someone tell me what the \.
and i
on the :with
-line in the following are used for?
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be a URL for GIF, JPG or PNG file.'
}
Upvotes: 4
Views: 231
Reputation: 10051
%r{}
is used for regular expressions.
\.
is looking for the literal character .
. You need the \
to escape because just using .
means something else entirely (any character matches).
i
is used for case insensitive searches.
Essentially, your regex is matching for anything that ends in .gif
, .jpg
or .png
. These could also be something like .GiF
because of the case insensitive search.
Upvotes: 7
Reputation: 118261
In Ruby, you can create Regexps using the %r{...} literals.
The following are metacharacters (, ), [, ], {, }, ., ?, +, *.
They have a specific meaning when appearing in a pattern. To match them literally they must be backslash-escaped.
i
is for case insensitive match.
Upvotes: 7