I am trying to create a regular expression in javascript that will match these requirements
So far I have come up with
^[0][1-9][\d ]{9,10}$
however, this would still match with 2 spaces or no spaces. 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)(?=\S* \S*$)
- there must be 1 space only (just a lookahead check, fails the match if the condition is not met)0
- the first char matched must be 0
[ 1-9]
- the second char is any digit but 0
or space [ \d]+
- 1 or more digits or spaces$
- end of string.You can use this regex with positive lookahead:
/^0[1-9](?=\d* \d+$)[ \d]{9,11}$/
RegEx Demo
(?=\d* \d+$)
ensures there is at least a space after first characters{9,11}
as 2 characters have already been matched at start (zero and non-zero)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With