I need help creating a simple regex for a whole number range of 1-1000, with no special characters.
The two I have both seem to break or allow characters or not the full range:
^\d(\d)?(\d)?$
^[0-9]{1,3}$
Try this:
^([1-9][0-9]{0,2}|1000)$
[1-9][0-9]{0,2}
matches any number between1
–999
1000
matches1000
Use ^(.*[^0-9]|)(1000|[1-9]\d{0,2})([^0-9].*|)$
which will match 1000
or a non-zero digit followed by up to two further digits. It will also allow other characters on either end of the number.
精彩评论