简体   繁体   English

JavaScript中的正则表达式以检查日期和时间

[英]Regular expression in javascript to check time with date

I need to check whether the text contains a date part with it. 我需要检查文本是否包含日期部分。

for ex: mytext_12/26/2011_11:51_AM or someText_12/26/2011_13:51_PM have a date part it returns true. 例如: mytext_12/26/2011_11:51_AM or someText_12/26/2011_13:51_PM的日期部分返回true。

I am not too good with expressions so looking for one. 我对表达不太满意,因此寻找一个。 the format of the date - time part is fixed. 日期-时间部分的格式是固定的。

i got a way out for it but failing for time . 我为此找到了出路,但是却失败了。

var containsDate = ~str.search(/\d{1,2}\/\d{1,2}\/\d{4}/);

it checks the date part perfectly but when i am trying for the time part i am messing it up some where . 它完美地检查了日期部分,但是当我尝试时间部分时,我把它弄乱了。

_\d{1,2}:\d{2}_(?:AM|PM) this is the part for time but i am not able to generate the final regex by combining this two.

Try, 尝试,

.search(/[0-9]{2}\/[0-9]{2}\/[0-9]{4}_[0-9]{2}:[0-9]{2}_(AM|PM)/)

A good resource for regex, MDN:Regular Expressions MDN:Regular Expressions是正则表达式的良好资源

尝试,

.search(/\d+\/\d+\/\d{4}_\d+:\d+_(AM|PM)/)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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