Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone give me a regular expression for a number to be between 1 and 5

Tags:

regex

Can someone give me a regular expression for a number to be between 1 and 5, single digit

e.g. input has to be a number between 1 and 5 , 55 or 23 would not match

like image 712
umarali Avatar asked Sep 02 '25 09:09

umarali


2 Answers

Try using anchors:

/^[1-5]$/

Explanation:

^     Start of line/string.
[1-5] A digit between 1 and 5.
$     End of line/string.
like image 57
Mark Byers Avatar answered Sep 05 '25 01:09

Mark Byers


Would it not be simpler to check it as a number (ie if(x>=1 && x<=5) or something similar) rather than using a regex?

like image 40
Spudley Avatar answered Sep 05 '25 01:09

Spudley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!