使用JavaScript中的正则表达式解析日期 [英] Date parsing with regular expressions in JavaScript

查看:119
本文介绍了使用JavaScript中的正则表达式解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中使用match()来解析RSS提要中的日期,我无法让我的头靠近正确的正则表达式来查找日期格式。

I'm using match() in JavaScript to parse a dates from an RSS feed, I just can't get my head around the correct regular expression to find the date format.

这是日期:

2009-05-11 16:59:20

到目前为止的正则表达式:

And the regular expression so far:

if (dateToParse.match(/^\d\d\d\d-\d\d-\d\d/)) {
        dateTimeSeparator = " ";
        monthIndex = 0;
        dayIndex = 1;
        yearIndex = 2;
}


推荐答案

/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/

这将日期放在第一到第三组,第四至第六组的时间。

This puts the date in the first to third groups, and the time in the forth to sixth groups.

这篇关于使用JavaScript中的正则表达式解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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