RegEx to allow letters numbers and spaces but not two spaces in a row

孤者浪人 提交于 2021-01-29 02:52:16

问题


Hello I have a RegEx that allows up to 18 characters with numbers, letters and spaces only.

How can I modify this to check if the string is starting with a space, or has two spaces in a row?

/^[0-9A-Za-z\s]{1,18}$/

I am using this regex in both native javascript (not jquery) and PHP.


回答1:


You can use a negative look-ahead to check if the string starts with a space or has 2 or more consecutive spaces, something like:

^(?!.*\s{2,})(?!^ )[0-9A-Za-z\s]{1,18}$



回答2:


(?:(?![ ]{2}).)+

use this regex. This may solve your problem.



来源:https://stackoverflow.com/questions/36902258/regex-to-allow-letters-numbers-and-spaces-but-not-two-spaces-in-a-row

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