Regex to check alphanumeric string in ruby

前端 未结 6 2024
生来不讨喜
生来不讨喜 2021-02-19 21:40

I am trying to validate strings in ruby. Any string which contains spaces,under scores or any special char should fail validation. The valid string should contain only chars a-z

6条回答
  •  情话喂你
    2021-02-19 22:25

    If you are validating a line:

    def validate(string)
      !string.match(/\A[a-zA-Z0-9]*\z/).nil?
    end
    

    No need for return on each.

提交回复
热议问题