Search code examples
javascriptregexdigits

A limit with one or two numeric digits Regex


I am trying to figure out how to create a regex that will let me search for not only one or double digits, but also have a limit from 1-60. I want double digits to be applicable as well so for example, 01-09 works just as well as 1-9.

^([1,2,3,4,5]\d{0,1}|(60))

is what I have so far in terms of setting up the the double digits, but I then can't get 7, 8, 9 to pass as either single or double digits. Any idea on how to solve this problem or do I need to do a case by case approach?


Solution

  • try this;

    ^0*([1-9]$|^[1-5][0-9]$|^60$)
    

    from this generator

    ie, can either be just 1-9, or 1-5 followed by 1-9, or 60 itself.

    Edit amended to force begining and end of string and allows leading zeros