简体   繁体   中英

support AM/PM time format as well as 24 hour

I've got a regex like so:

/(\d\d?[\/-]\d\d?[\/-]\d{2,4}) (\d\d:\d\d:\d\d): ([^:]+): (.*)/g

this currently supports a string that looks like this: 16/05/14 09:38:22: Jons Janssen: Yo.

I need it to support 16/05/14 11:19:33 pm: Jons Janssen: Yo Bas. As well.

How do I do this?

Change your regex like so:

(\d\d?[\/-]\d\d?[\/-]\d{2,4}) (\d\d:\d\d:\d\d(?: [ap]m)?): ([^:]+): (.*)

(?: [ap]m)? is a non-capturing group that matches either am or pm . The ? at the end indicates that the whole group is optional.

Regex demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM