gpt4 book ai didi

ruby-on-rails - 正则表达式 : only string can be accepted nothing else ?

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:40 24 4
gpt4 key购买 nike

我正在开发 ruby on rails 这里是 regex

的代码
string_regex = /[a-z]+\z/i
validates :name , :format => { :with => string_regex , :message => "should not contain special character" }

尝试:

  1. dbcda->真
  2. abjdkbcak->真
  3. jgh1213 -> 错误
  4. 1314134@#$ -> 错误
  5. jbh31$ -> 错误
  6. ,,,,,,, -> false
  7. kjgh,g,, -> TRUE(应该是 false)。

最佳答案

您需要使用 /\A[a-z]+\z/i 因为您不希望从字符串开头 (^) 到结尾 (\z)

如需帮助,请尝试 http://rubular.com/

[编辑] 如@2called-chaos 所述更改

关于ruby-on-rails - 正则表达式 : only string can be accepted nothing else ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22788372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com