1

I want to make user enter time in format "hh:mma" or "hh:mm a". I want to leave it to user the choice to enter space between mm & a. What can be the regular expression for this ?

4 Answers4

0

Use ? to make the preceding token optional, for example /colou?r/ matches color and colour. If you are struggling with the hh:mm part of the regex, this may help.

Community
  • 1
  • 1
alexdavey
  • 672
  • 1
  • 9
  • 15
0

Try this:

(([0-1]?[0-9])|2[0-3]):[0-5][0-9] ?(am|pm|AM|PM)
Koen Peters
  • 12,798
  • 6
  • 36
  • 59
0

Something like the following should work

([01]?[0-9]|2[0-3]):([0-5]?[0-9])\s?(am|pm)
Jonathon
  • 15,873
  • 11
  • 73
  • 92
0

I just put space in the pattern where I required space and if worked perfectly