[英]Regular Expression to Match Character and Max Length
I am trying to create a regular expression in javascript that will match these requirements 我试图在javascript中创建一个符合这些要求的正则表达式
So far I have come up with ^[0][1-9][\\d ]{9,10}$
到目前为止,我已经提出了
^[0][1-9][\\d ]{9,10}$
however, this would still match with 2 spaces or no spaces. 但是,这仍然会匹配2个空格或没有空格。 I have been so far unsuccessful to find a way to require just one space anywhere within the number.
到目前为止,我找不到在数字中任何地方只需要一个空格的方法是不成功的。 I have tried positive lookahead and found other examples online, however making the quantifier work with the requirement has proven difficult.
我尝试过积极的前瞻,并在网上找到了其他的例子,但是量化器的工作量已经证明是困难的。
You can use 您可以使用
/^(?=.{11,12}$)(?=\S* \S*$)0[ 1-9][ \d]+$/
See the regex demo 请参阅正则表达式演示
Details: 细节:
^
- start of string ^
- 字符串的开头 (?=.{11,12}$)
- total length must be 11 to 12 chars (just a lookahead check, fails the match if the condition is not met) (?=.{11,12}$)
- 总长度必须是11到12个字符(只是前瞻检查,如果条件不满足则不匹配) (?=\\S* \\S*$)
- there must be 1 space only (just a lookahead check, fails the match if the condition is not met) (?=\\S* \\S*$)
- 必须只有1个空格(只是前瞻检查,如果条件不满足则不匹配) 0
- the first char matched must be 0
0
- 匹配的第一个char必须为0
[ 1-9]
- the second char is any digit but 0
or space [ 1-9]
- 第二个字符是任何数字,但为0
或空格 [ \\d]+
- 1 or more digits or spaces [ \\d]+
- 1个或多个数字或空格 $
- end of string. $
- 结束字符串。 You can use this regex with positive lookahead: 你可以使用正面前瞻这个正则表达式:
/^0[1-9](?=\d* \d+$)[ \d]{9,11}$/
(?=\\d* \\d+$)
ensures there is at least a space after first characters (?=\\d* \\d+$)
确保在第一个字符后至少有一个空格 {9,11}
as 2 characters have already been matched at start (zero and non-zero) {9,11}
作为2个字符已经在开始时匹配(零和非零) Here's my attempt. 这是我的尝试。 It's slightly faster than Wiktors.
它比Wiktors 稍快。 And since the time is already spent, I'll share it.
由于已经花了时间,我会分享它。
^(?=\d* \d*$)0[1-9 ][\d ]{9,10}$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.