How to validate a user name with regex?

后端 未结 10 1741
礼貌的吻别
礼貌的吻别 2020-11-27 12:34

This seems to match the rules I have defined, but I only starting learning regex tonight, so I am wondering if it is correct.

Rules:

  • Usernames can cons
10条回答
  •  鱼传尺愫
    2020-11-27 13:40

    Your regex doesn't work. The hard part is the check for consecutive spaces/hyphens. You could use this one, which uses look-behind:

    /^[a-zA-Z0-9][a-zA-Z0-9_\s\-]*[a-zA-Z0-9](?

提交回复
热议问题