To regex only numbers that do not exceed 1000 and exclude 0 in JavaScript, you can use the following regular expression:
index.tsx38 chars2 lines
Let's break it down:
^
asserts the start of the string(?!0$)
negative lookahead to exclude just a single '0'(?!0\d)
negative lookahead to exclude '0' followed by any digit([1-9]|[1-9]\d|1000)
matches either a single digit from 1 to 9, or two digits from 10 to 99, or 1000$
asserts the end of the stringHere's how you can use this regular expression in JavaScript:
index.tsx461 chars15 lines
In the above code snippet, we create a regular expression object using the RegExp
constructor and test it against various numbers using the match
method. Only numbers that match the regular expression are returned and all others return null
.
gistlibby LogSnag