javascript regex iso datetime [英] javascript regex iso datetime

查看:105
本文介绍了javascript regex iso datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有一个很好的regex模式匹配iso数据时间?



ie:2010-06-15T00:00:00

解决方案

对于严格的,完整的日期时间,包括毫秒,每 W3C的规范 。:

  //  -  - 完整的精度:
/ \d {4} - [01] \d- [0-3] \dT [0-2] \d:[0-5] \d:[ 0-5] \d\.\d +([+ - ] [0-2] \d:[0-5] \d | Z)/

// - - 没有毫秒:
/ \d {4} - [01] \d- [0-3] \dT [0-2] \d:[0-5] \d:[ 0-5] \d([+ - ] [0-2] \d:[0-5] \d | Z)/

// - 无秒数:
/ \d {4} - [01] \d- [0-3] \dT [0-2] \d:[0-5] \d([+ - ] [0- 2] \d:[0-5] \d | Z)/

// - 把它们放在一起:
/(\d {4} - [01 ] \d- [0-3] \dT [0-2] \d:[0-5] \d:[0-5] \d\.\d +([+ - ] [0-2] \d:[0-5] \d | Z))|(\d {4} - [01] \d- [0-3] \\ dT [0-2] \d:[0-5] \d:[0-5] \d([+ - ] [0-2] \d:[0-5] \d | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |]]]]]]]]]]]]]]]]]]]]]] [0-2] \d:[0-5] \d | Z))/



实际的 ISO 8601:2004(E)文档允许的其他变体

  / ******************** ************************ 
**无时区变量:
* /
// - 完成精度:
/ \d {4} - [01] \d- [0-3] \dT [0-2] \d:[0-5] \d:[0 -5] \d\.\d + /

// - 无毫秒:
/ \d {4} - [01] \d- [0- 3] \dT [0-2] \d:[0-5] \d:[0-5] \d /

// - 无秒数:
/ \d {4} - [01] \d- [0-3] \dT [0-2] \d:[0-5] \d /

// - 把它们放在一起:
/(\d {4} - [01] \d- [0-3] \dT [0-2] \d:[0-5 ] \d:[0-5] \d\.\d +)|(\d {4} - [01] \d- [0-3] \dT [0-2] \\ \\d :[0-5] \d:[0-5] \d)|(\d {4} - [01] \d- [0-3] \dT [0-2] \ d:[0-5] \d)/

警告:这一切都会变得很快,它仍然允许某些废话,如第十四个月。
此外,ISO 8601:2004(E)允许其他几种变体。





2010-06-15T00:00:00不合法,因为没有时间-zone name。


does anyone have a good regex pattern for matching iso datetimes?

ie: 2010-06-15T00:00:00

解决方案

For the strict, full datetime, including milliseconds, per the W3C's take on the spec.:

//-- Complete precision:
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/

//-- No milliseconds:
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)/

//-- No Seconds:
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)/

//-- Putting it all together:
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/

.
Additional variations allowed by the actual ISO 8601:2004(E) doc:

/********************************************
**    No time-zone varients:
*/
//-- Complete precision:
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+/

//-- No milliseconds:
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d/

//-- No Seconds:
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d/

//-- Putting it all together:
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+)|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d)|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d)/

WARNING: This all gets messy fast, and it still allows certain nonsense such as a 14th month. Additionally, ISO 8601:2004(E) allows a several other variants.

.
"2010-06-15T00:00:00" isn't legal, because it doesn't have the time-zone designation.

这篇关于javascript regex iso datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆