Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are `\.` and `i` in a regular expression?

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.'
}
like image 547
rhys97 Avatar asked Dec 09 '22 02:12

rhys97


1 Answers

%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.

like image 54
Justin Wood Avatar answered Dec 24 '22 21:12

Justin Wood