Search code examples
regexregex-lookarounds

Regex to allow only number between 1 to 12


Regex to allow only number between 1 to 12

I am trying (12)|[1-9]\d? but its not working, please help as i am new to regular expression


Solution

  • Something like

    ^([1-9]|1[012])$
    
    • ^ Anchors the regex at start of the string
    • [1-9] Matches 1 to 9

    • | Alternation, matches the previous match or the following match.

    • 1[012] Matches 10, 11, or 12
    • $ Anchors the regex at the end of the string.

    Regex Demo