EasyRegex Logo
EasyRegex
New

Match Time in h:mm or hh:mm Format

Javascript

Regular Expression

\b(?:1[0-2]|0?[1-9]):[0-5][0-9]\b
Copy
Do you have Feedback?

Explanation

This regex pattern matches time in either h:mm or hh:mm format. It starts with an optional 0 for single-digit hours, followed by either 1 or 2 for double-digit hours. Then, a colon is required, followed by minutes ranging from 00 to 59. The \b ensures that the match is a whole word.

Javascript

Example Usage

const timeRegex = /\b(?:1[0-2]|0?[1-9]):[0-5][0-9]\b/;
const time = '9:30';
if (time.match(timeRegex)) {
    console.log('Valid time format');
} else {
    console.log('Invalid time format');
}
Copy
Test the expression

Similar Regular Expressions