开发者

Regex for Google Calendar API date output

开发者 https://www.devze.com 2023-03-21 00:30 出处:网络
I have a string containing a date which has a lot of format possibilities.My regular expression to match it is getting ridiculous and long. I\'m hoping to learn a more concise expression. Here are the

I have a string containing a date which has a lot of format possibilities. My regular expression to match it is getting ridiculous and long. I'm hoping to learn a more concise expression. Here are the various formats. I'd like to get each date as its own match:

Sat Jul 16, 2011
Sat Jul 16, 2011 6pm to 8pm
Sat Jul 16, 2011 6pm to 8:30pm
Sat Jul 16, 2011 6:30pm to 8pm
Sat Jul 16, 2011 6:30pm to 8:30pm

Sat Jul 16, 2011 to Sun Jul 17, 2011
Sat Jul 16, 2011 6pm to Sun Jul 17, 2011 8pm
Sat Jul 16, 2011 6:30pm to Sun Jul 17, 2011 8pm
Sat Jul 16, 2011 6pm to Sun Jul 17, 2011 8:30pm
Sat Jul 16, 2011 6:30pm to Sun Jul 17, 2011 8:30pm

Before I came here to ask, I'd gotten to this point:

([A-Z]{1}[a-z]{2} [A-Z]{1}[a-z]{2} [0-9]{1,2}, [0-4]{4} [0-9]{1,2}[ap]{1}m)( to ([A-Z]{1}[a-z]{2} [A-Z]{1}[a-z]{2} [0-9]{1,2}, [0-4]{4} [0-9]{1,2}[ap]{1}m))?

Which would return the first date, and the second if it exists, but only for this format: Sat Jul 16, 2011 6pm to Sun Jul 17, 2011 8pm. That got absurd.

Edit:

Someone suggeste开发者_如何学Cd I check for the "to" portion of the date, and run the regex on each side, rather than trying to account for all cases at once. Still looking for a more concise solution to each side of it, but that will help a lot.


Break the date into two halves as suggested, and then use date.js to parse both halves. Then it's Date.parse('Sat Jul 16, 2011 6:30pm')

0

精彩评论

暂无评论...
验证码 换一张
取 消