Regular expression to validate username

后端 未结 10 973
温柔的废话
温柔的废话 2020-11-28 19:27

I\'m trying to create a regular expression to validate usernames against these criteria:

  1. Only contains alphanumeric characters, underscore an
10条回答
  •  没有蜡笔的小新
    2020-11-28 19:54

    I guess you'd have to use Lookahead expressions here. http://www.regular-expressions.info/lookaround.html

    Try

    ^[a-zA-Z0-9](_(?!(\.|_))|\.(?!(_|\.))|[a-zA-Z0-9]){6,18}[a-zA-Z0-9]$

    [a-zA-Z0-9] an alphanumeric THEN (

    _(?!\.) a _ not followed by a . OR

    \.(?!_) a . not followed by a _ OR

    [a-zA-Z0-9] an alphanumeric ) FOR

    {6,18} minimum 6 to maximum 18 times THEN

    [a-zA-Z0-9] an alphanumeric

    (First character is alphanum, then 6 to 18 characters, last character is alphanum, 6+2=8, 18+2=20)

提交回复
热议问题