Bob2u
Bob2u

Reputation: 135

JavaScript RegEx (0-100) but not leading 0

I am trying to get a RegEx working for 0-100% including 0, but not as a leading number. EXAMPLE valid inputs would be 0, 10, 34, 100 and invalid would be 00, 01, 045, 000, or any number larger than 100.

I'm finding a lot of examples with 1-100 on here but not single 0, and my lack of understanding RegEx isn't helping me make the proper adjustments. Thanks for help.

Upvotes: 1

Views: 172

Answers (1)

anubhava
anubhava

Reputation: 785661

You can use this regex:

^(?:\d|[1-9]\d|100)$

RegEx Demo

RegEx Breakup:

  • ^: Start
  • (?:: Start non-capturing group
    • \d: match numbers from 0 to 9
    • |: OR
    • [1-9]\d: match numbers from 10 to 99
    • |: OR
    • 100: match 100
  • ): End non-capturing group
  • $: End

Upvotes: 6

Related Questions