I have the following regex that uses look-behind to match different time formats inside a string:
(?:(?<=[\s])|^)(2[0-3]|[01]?[0-9]):([0-5]?[0-9])(?::([0-5]?[0-9]))?( ?[AP]M)?(?:(?=[\s])|$)
The regex must match 12-hour or 24-hour time formats, with minutes and optional seconds. Examples:
10:00 AM
Time: 10:00 (only the `10:00` part)
13:23:34
The regex should entirely reject the following strings:
hello10:00 (the time is not separated by whitespace)
24:00:00 (24 does not make sense)
9912:1299 (there's a time substring, but the whole string is not really a time).
I'm trying to rewrite it to not use positive look-behind, which is not supported by every web browser. How could I do it?
\b(?:1?\d|2[0-3]):[0-5]\d(?::[0-5]\d)? ?(?:[ap]m)?\b