[Solved] username regex in rails 4 [closed]


You could use this regex:

^(\w|\.)+$

Which is the same as:

^[a-zA-Z0-9_\.]+$

Here’s preview of the regex in action on regex101.com, and here’s a breakdown of it

  • ^ matches the beginning of the string
  • ( just groups the characters so a modifier can be applied
  • \w matches any character that is a-z, A-Z, 0-9, and _. Making it the same as [a-zA-Z0-9_].
  • | is the OR character, making it so either the match behind or in-front of it is found.
  • \. matches literally ..
  • ) ends the character group
  • + makes it so the previous character group is matched once or more
  • $ matches the end of the string

If you wanted to disallow . and _ in the beginning and end of the username, you could use:

^[a-zA-Z0-9](\w|\.)*[a-zA-Z0-9]$

Make sure to use the :multiline => true option, to avoid errors.

4

solved username regex in rails 4 [closed]