I have a reqular expression that matches HH:MM e.g. 12:23 and it is:
function IsValidTime(timeString)
{
var pattern = /^\d?\d:\d{2}$/;
if (!timeString.match(pattern))
return false;
}
How do I change this line:
var pattern = /^\d?\d:\d{2}$/;
to check for a string that is formatted with seconds like so: HH:MM:SS e.g. 12:23:05
/^(?:2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/
for 24-hour time, leading zeroes mandatory.
/^(?:2[0-3]|[01]?[0-9]):[0-5][0-9]:[0-5][0-9]$/
for 24-hour time, leading zeroes optional.
/^(?:1[0-2]|0[0-9]):[0-5][0-9]:[0-5][0-9]$/
for 12-hour time, leading zeroes mandatory.
/^(?:1[0-2]|0?[0-9]):[0-5][0-9]:[0-5][0-9]$/
for 12-hour time, leading zeroes optional.
Something as simple as the following should work:
/([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]/g
([01][0-9]|2[0-3])
[01][0-9] the characters "0" or "1" followed by any digit between 0 and 9| - or2[0-3] the character "2" followed by a digit between 0 and 3: a literal colon[0-5][0-9] - any digit between 0 to 5 followed by any digit between 0 and 9: a literal colon[0-5][0-9] - any digit between 0 to 5 followed by any digit between 0 and 9Regex101
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With