How to translate ruby regex to javascript? - (?i-mx:..) and Rails 3.0.3

前端 未结 3 1384
自闭症患者
自闭症患者 2021-01-01 18:34

Im using validates_format_of method to check email format:

validates_format_of :email, :with => /^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$/i
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 19:02

    The reason is that you are converting your regular expression using .to_s instead of .inspect. What you need to do in your view is use .inspect to get the proper format. Here is some sample code that should explain the issue:

    email = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
    email.to_s #"(?i-mx:^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$)"
    email.inspect #"/^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$/i"
    

    so, in your javascript view do something like this to get the actual string representation you want:

    <%= email.inspect %>
    

提交回复
热议问题