regex to match a username with no consecutive spaces

ぃ、小莉子 提交于 2021-02-02 09:37:58

问题


I am struggling to make a javascript regex to satisfy the following:

  1. The first character has to be alphabetical ([a-zA-Z])
  2. The rest can be any letters, any numbers, hyphen, dot, underscore and spaces
  3. BUT no consecutive spaces, e.g: two or more spaces in a row
  4. The length has to be between 3 and 25 (inclusive)

So here is what I found Regex:

/^[a-z][\s\w.-]{3,24}$/i

My current regex works, but won't be able to test whether the user has written consecutive spaces. How can I test for that?


回答1:


This should work for you:

/^[a-z](?!.* {2})[ \w.-]{2,24}$/gmi

RegEx Demo



来源:https://stackoverflow.com/questions/28751997/regex-to-match-a-username-with-no-consecutive-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!