Search code examples
javascriptregexroman-numerals

How to create regular expression checking Roman numerals?


I need to create regular expression which verifies if user inputs:

  • 4 digits OR
  • value like XXXXXX-YY, where X is roman numerals from I to XXXIII and YY is two latin characters (A-Z)

Solution

  • According to the requirements, these are possible roman number-formats. For readability, only the maximum number of X is shown.

    XXX III     (or: <empty>, I or II instead of III)
    XX V       (or: IV, IX and X instead of IV)
    

    I suggest this compact pattern:

    /^(\d{4}|(?=[IVX])(X{0,3}I{0,3}|X{0,2}VI{0,3}|X{0,2}I?[VX])-[A-Z]{2})$/i
    

    Explanation:

    ^                Begin of string
    (                Begin of group 1.
      \d{4}             4 digits
    
    |                 OR
    
      (?=[IVX])         Look-ahead: Must be followed by a I, V or X
      (                  Begin of group 2.
         X{0,3}I{0,3}       = 0 1 2 3  + { 0 ; 10 ; 20 ; 30} (roman)
      |                  OR
         X{0,2}VI{0,3}      = 5 6 7 8  + { 0 ; 10 ; 20 }     (roman)
      |                  OR
         X{0,2}I?[VX]       = 4 9      + { 0 ; 10 ; 20 }     (roman)
      )                  End of group 2
      -[A-Z]{2}          Postfixed by a hyphen and two letters
    )                 End of group 1.
    $                End of string