简体   繁体   中英

Javascript Regular expression to get the Time from the string text

I want to detect a string of text to get the time from the content of my page using javascript. Regular expression should be able to match all occurrences of the below strings.

  • 10:00 PM CST
  • 12/10/2014 05:00 AM IST
  • 11-02-2015 23:00 CST
  • 05/2/2015 10:00 PM EST
  • 23rd Jan 2015 02:00 IST
  • 04:00AM CST

I have tried with the following two regular expressions but they does not cover all the scenarios yet.

Time Match

[0-9][0-9]:[0-9][0-9] (am|pm|AM|PM)

Date Match

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|
(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|
(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|
(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|
[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|
[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|
[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|
(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|
(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

I have gotten too far into this using RegEx, but it matches some of the examples above just fine. Any suggestions on how I should proceed?

Please try:

/((?:(?:\d{1,2}(?:\/|-)){2}\d{4}\s+|\d{2}(?:rd|th|nd)\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{4}\s+|)\d{2}:\d{2}(?:\s*(?:AM|PM))?\s+[IEC]ST)/g

REGEX 101 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