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

前端 未结 3 1353
自闭症患者
自闭症患者 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 18:53

    I've written a small gem that translates Ruby regexes to JavaScript:

    https://github.com/janosch-x/js_regex

    It can handle more cases than the gsub approach, and includes warnings if any incompatibilities remain.

    Note that, whatever you do, not all Ruby regexes can be fully translated to JS, because Ruby's regex engine has a lot of features that JavaScript's doesn't have.

提交回复
热议问题