我一直在尝试使用JavaScript中的Google日历xml Feed来匹配各种日期,但我失败了。
目前我有这个正则表达式:
\s(.*)\s
以下是一些字符串示例(每个代码块都是一个字符串),我需要在其中获取匹配项(请注意斜杠和换行符):
字符串1:
When: Thu Jan 9, 2014 1:50pm to 2:45pm
GMT<br />
<br />Where: Hall A
<br />Event Status: confirmed</content>
字符串2:
When: Mon Jan 4, 2014<br />
<br />Event Status: confirmed</content>
字符串3:
When: Mon Oct 27, 2013 to Fri Nov 1, 2013
<br />
<br />Event Status: confirmed</content>
字符串4:
When: Tue Oct 15, 2013 3:30pm to 7:30pm
BST<br />
<br />Event Status: confirmed
<br />Event Description: Please complete a booking.&#39;s teacher.</content>
字符串5:
When: Mon Apr 21, 2014 to Fri Apr 25, 2014
<br />
<br />Event Status: confirmed</content>
所以我想只匹配日期格式为“Day Mon dd,year”或“Day Mon dd,year time”。之后我不想要这个部分。我不需要“到”部分。
因此,如果存在一个有时间的日期,它应该匹配,如果时间不存在那么它应该只匹配日期。
我是正则表达式的新手,并不知道如何解决这个问题。我打算拿起一本书并正确学习,但我必须尽快解决这个问题。
如果有人能帮助我,我会非常感激。
答案 0 :(得分:3)
由于您不熟悉RegExp,请告诉您有两种使用正则表达式的方法。
new RegExp('pattern-here')
使用javascript语法/pattern-here/flags
第二个很容易:)
答案是
var regex = /(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s([0-9][0-9]|[0-9]),\s\d{4}\s([0-9][0-2]|[0-9]):([0-6][0-9]|[0-6]{2})(pm|am)|(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s([0-9][0-9]|[0-9]),\s\d{4}/g
这就是你所要求的。
abc123的答案与时间不符,而且与
等非法日期相匹配Hel Som 99, 9999
但是我没有。
使用它:
var matchDateRegex= /(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s([0-9][0-9]|[0-9]),\s\d{4}\s([0-9][0-2]|[0-9]):([0-6][0-9]|[0-6]{2})(pm|am)|(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s([0-9][0-9]|[0-9]),\s\d{4}/gm;
var s="some string with date Mon Jan 22, 2013 some thing Mon Jan 22, 2013 1:50pm";
var dates= s.match(matchDateRegex);
这将返回一个匹配日期数组,存储在dates
变量中。
现在您可以按索引访问日期。
注意:如果您只想使用不带 javascript语法的模式,请先删除 字符和最后三个字符('/'和'/ gm'),但是你需要做
var regex=new RegExp('(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s([0-9][0-9]|[0-9]),\s\d{4}\s([0-9][0-2]|[0-9]):([0-6][0-9]|[0-6]{2})(pm|am)|(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s([0-9][0-9]|[0-9]),\s\d{4}','gm');
如果有帮助,请将其标记为回答。 (我相信它会)
答案 1 :(得分:2)
<强> Regex: 强>
When: ([\w]{3}) ([\w]{3}) ([\d]{1,2}), ([\d]{4})
正则表达式解释
/When: ([\w]{3}) ([\w]{3}) ([\d]{1,2}), ([\d]{4})/
When: matches the characters When: literally (case sensitive)
1st Capturing group ([\w]{3})
[\w]{3} match a single character present in the list below
Quantifier: Exactly 3 times
\w match any word character [a-zA-Z0-9_]
matches the character literally
2nd Capturing group ([\w]{3})
[\w]{3} match a single character present in the list below
Quantifier: Exactly 3 times
\w match any word character [a-zA-Z0-9_]
matches the character literally
3rd Capturing group ([\d]{1,2})
[\d]{1,2} match a single character present in the list below
Quantifier: Between 1 and 2 times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
, matches the characters , literally
4th Capturing group ([\d]{4})
[\d]{4} match a single character present in the list below
Quantifier: Exactly 4 times
\d match a digit [0-9]
注意: 如果他们向您发送XML ...请使用XML解析器而不是RegEx,如果您想指定后端语言我可以协助您还可以使用JavaScript XML解析XML to JavaScript Object