简体   繁体   English

将正则表达式中的字符串设置为带有空格的限制

[英]Set limit to string in regular expression with white spaces

I have this regular expression 我有这个正则表达式

/^([a-zA-Z01-9]+ ?){0,20}$/

I have a set of rules 我有一套规则

  • The string line can't start with a white space. 字符串行不能以空格开头。
  • The string cannot be longer than 20 characters including the white spaces . 该字符串不能超过20个字符, 包括空格
  • The string cannot have two white spaces consecutively. 该字符串不能连续包含两个空格。
  • The string can ends with a white space. 字符串可以以空格结尾。

The only problem that I have is to set the length of the string including the white space to 20 as maximum. 我唯一的问题是将包括空白在内的字符串的长度最大设置为20。

试试这个: ^(?=[a-zA-Z\\d\\s]{0,20}$)([a-zA-Z01-9]+ ?)*$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM