Regex for time (24-hour)

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

Options
Matches

Examples

Note

Requires two-digit hours. Allow 9:05 by changing [01]\d to [01]?\d.

Matches in the example

  • "09:00"
  • "17:30:15"

Token by token

  1. \b a word boundary
  2. (?: a group, not capturing
  3. [01] one of: “0” or “1”
  4. \d a digit (0–9)
  5. | or
  6. 2 the character “2”
  7. [0-3] one of: “0” to “3”
  8. ) end of group
  9. : the character “:”
  10. [0-5] one of: “0” to “5”
  11. \d a digit (0–9)
  12. (?: a group, not capturing
  13. : the character “:”
  14. [0-5] one of: “0” to “5”
  15. \d a digit (0–9)
  16. )? end of group, whole group optionally
  17. \b a word boundary

Other patterns