My specific requirement is to validate a string ranging from 1 to 4 characters in length (where H stands for hour and M represents minutes):
If the string has 1 character, it should be in the format of H (a digit between 0-9).
A 2-character string should look like HH (the first H can be between 0-2 and the second one between 0-9; however, if the first H is 2, the second one can only be 0-4).
For a 3-character string, the format should be HMM (first H: between 0-9, first M: between 0-5, second M: between 0-9).
A 4-character string should have the format HHMM (first H: between 0-2, second H: between 0-9; but when the first H is 2, the second H can only be 0-4. Additionally, first M: between 0-5, and second M: between 0-9).
The challenge here lies in not knowing the length of the string beforehand and the same number signifying different things depending on its length. Any suggestions on using JavaScript Regular Expressions to handle this validation?