The provided regular expression is using multiline anchors (^ or $)

前端 未结 1 1411
無奈伤痛
無奈伤痛 2020-12-31 06:12

I trying to write a Image validation format that makes sure url ends with either .png, .jpg or .gif .

class Product < ActiveRecord::Base

  mount_uploader         


        
相关标签:
1条回答
  • 2020-12-31 06:33

    ^ and $ are both line anchors. If a user were to pass in a string with http://www.foo.com/bar.png\nfoo_bar_baz!, then your regex is going say that the input is valid because it will match .png to the newline, which is not what you want.

    Change your regex above to be %r{\.(gif|jpg|png)\z}i instead. The \z is an end of string anchor, which is what you want instead of the end of line anchor.

    There are some great answers on another, very similar question: Difference between \A \z and ^ $ in Ruby regular expressions.

    0 讨论(0)
提交回复
热议问题