Home > Software engineering >  how i set this pattern regex to the input?
how i set this pattern regex to the input?

Time:10-21

How I set this pattern to the regex input?

  • until three number digits only ex: 0, 1, 12, 123, 555, 123, 456,102,10

  • accept zero, but just in the begginin without next numbers: this is ok >> 0

  • but not this >> 01, 05,06, or 066, 072;

Anyone can Help?

CodePudding user response:

Try to use this function

const​ ​zeroPad​ ​=​ ​(​number​,​ ​length​ ​=​ ​2​)​ ​=>​ ​{ 
 ​    ​number​ ​=​ ​String​(​number​)​; 
  
 ​    ​while​ ​(​number​.​length​ ​<​ ​length​)​ ​{ 
 ​        ​number​ ​=​ ​`0​${​number​}​`​; 
 ​    ​} 
  
 ​    ​return​ ​number​; 
}

Number is your number argument, Length is your number length

CodePudding user response:

^(?!0.)\d{1,3}$

1 to 3 digit numbers. Except numbers with more than one digit whose first digit is 0: ^(?!0.)

  • Related