I am trying to make sure that the format people input is exactly this :
.match(/\d{1,2}:\d\d\s((AM)|(PM))/)
Meaning that a user could write :
12:30 AM
2:30 PM
But not :
1:2 A
1:30
PM
It needs to be first two digits, followed by a colon, than two more digits, a space, and either AM or PM. But my regex expression isn’t that. What am I missing?
What exactly seems to be the problem?
However:
You need to ground your expression to the start (
^) and end ($) of the string otherwise;Look at
RegExp.test()instead, which returns a simplertrue/falserather than an array.A simpler expression which does the same thing could be
/^\d{1,2}:\d{2} [AP]M$/