Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate time with am and pm using JavaScript RegEx

Tags:

People also ask

How does regular expression validate time in 12-hour format?

On a 12-hour clock, if the first digit is 0, the second digit allows all 10 digits, but if the first digit is 1, the second digit must be 0, 1, or 2. In a regular expression, we write this as ‹ 1[0-2]|0?[1-9] ›.


I am not very good with regex. I have the following time: 12:00am.

I need a Javascript regex that respect this format: hh:mm[am/pm]

var regex = /^(\d\d):(\d\d)\s?(?:AM|PM)?$/;