DevTools
Back to Regex Tester

Time (HH:MM:SS) Regex Pattern

Match and validate time strings in HH:MM:SS and HH:MM formats using regex. Supports 24-hour format with optional seconds.

The Pattern

/\b(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?\b/g

How It Works

TokenMeaning
\bWord boundary to prevent partial matches
(?:[01]\d|2[0-3])Hours: 00-19 or 20-23
:Literal colon separator
[0-5]\dMinutes: 00-59
(?::[0-5]\d)?Optional seconds: colon followed by 00-59
\bTrailing word boundary

Test Cases

00:00:00Match
23:59:59Match
12:30Match
09:15:30Match
24:00:00No match
12:60:00No match
1:30No match

Code Examples

javascript
const 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"]
python
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']

Common Use Cases

Related Patterns

Want to test this pattern with your own data?

Try this pattern in Regex Tester →