/^\\d{1,2}[:][0-5][0-9]$/
is what I have. this limits minutes to 00-59. It does not, however, limit hours to between 0 and 12. For similarity and unifo
For folks looking for 24h format matching,
hh:mm:ss or h:mm:ss :
status = /^(2[0-3]|[0-1]?[\d]):[0-5][\d]:[0-5][\d]$/.test(timestr)
hh:mm or h:mm :
status = /^(2[0-3]|[0-1]?[\d]):[0-5][\d]$/.test(timestr)
This site is great for testing out: https://www.regexpal.com/
Addedum: Explanation for completeness:
^
: start of string, $
: end of string. So we put the expression in a ^..$
block to ensure there's nothing outside of our pattern.(2[0-3]|[0-1]?[\d])
: translates to 2[0-3]
OR [0-1]?[\d]
2[0-3]
: 20, 21, 22, 23[0-1]?[\d]
: 0 or 1 or nothing (?
) followed by any single digit(\d
). So, this works for numbers from 0 to 19.:
just that character[0-5][\d]
: number from 00 to 59. Note: this won't tolerate single-digit in the mm place.