regex number between 1-100 in javascript

Here's a regex pattern in JavaScript that matches numbers between 1 and 100:

index.tsx
/(^[1-9][0-9]?$|^100$)/
24 chars
2 lines

Explanation of the pattern:

  • / delimits the start and end of the regex pattern
  • ^ matches the beginning of the string
  • $ matches the end of the string
  • | acts as an OR operator
  • [1-9][0-9]? matches a number between 1 and 99
    • [1-9] matches any digit between 1 and 9
    • [0-9]? matches any digit between 0 and 9, with the question mark making it optional
  • ^100$ matches the number 100 exactly

gistlibby LogSnag