Match and validate time strings in HH:MM:SS and HH:MM formats using regex. Supports 24-hour format with optional seconds.
/\b(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?\b/g| Token | Meaning |
|---|---|
\b | Word boundary to prevent partial matches |
(?:[01]\d|2[0-3]) | Hours: 00-19 or 20-23 |
: | Literal colon separator |
[0-5]\d | Minutes: 00-59 |
(?::[0-5]\d)? | Optional seconds: colon followed by 00-59 |
\b | Trailing word boundary |
00:00:00Match23:59:59Match12:30Match09:15:30Match24:00:00No match12:60:00No match1:30No matchconst timeRegex = /\b(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?\b/g;
const text = "Meetings at 09:15:30, 14:00, and invalid 25:00";
const matches = text.match(timeRegex);
console.log(matches); // ["09:15:30", "14:00"]import re
time_regex = r'\b(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?\b'
text = "Meetings at 09:15:30, 14:00, and invalid 25:00"
matches = re.findall(time_regex, text)
print(matches) # ['09:15:30', '14:00']Want to test this pattern with your own data?
Try this pattern in Regex Tester →