Javascript日期正则表达式DD / MM / YYYY [英] Javascript date regex DD/MM/YYYY

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

问题描述

我知道那里有很多正则表达式线程我需要一个我无法在任何地方找到的特定模式

I know there are a lot of regex threads out there by I need a specific pattern I couldn't fin anywhere

这个正则表达式在YYYY-MM-验证DD格式

This regex validates in a YYYY-MM-DD format

/^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/

我需要将模式设为DD / MM / YYYY
(第一天,因为它是西班牙语,只有/, - 应该不允许)

I need the pattern to be DD/MM/YYYY (day first since it's in spanish and only "/", "-" should not be allowed)

我搜索了几个正则表达式库,我认为这个应该有效...但由于我不熟悉正则表达式,我不确定它是否有效喜欢那样

I searched several regex libraries and I think this one should work... but since I'm not familiar with regex I'm not sure it validates like that

(0[1-9]|[12][0-9]|3[01])[ \.-](0[1-9]|1[012])[ \.-](19|20|)\d\d

我也不知道为了逃避斜线,我试图看到字符串中的逻辑,但这就像是为我看矩阵代码。我将正则表达式字符串放在选项中.js

I also don't know ho to escape the slashes, I try to "see" the logic in the string but it's like trying "see" the Matrix code for me. I'm placing the regex string in a options .js

[...]  },
"date": {
                    "regex": (0[1-9]|[12][0-9]|3[01])[ \.-](0[1-9]|1[012])[ \.-](19|20|)\d\d,
                    "alertText": "Alert text AAAA-MM-DD"
                },
"other type..."[...]

所以,如果正则表达式没问题,我怎么逃避它?
如果不是,正确的正则表达式是什么以及如何逃避它? :P

So, if the regex is ok, how would I escape it? if it's not, what's the correct regex and how do I escape it? :P

非常感谢

推荐答案

我使用此功能dd / mm / yyyy格式:

I use this function for dd/mm/yyyy format :

// (new Date()).fromString("3/9/2013") : 3 of september
// (new Date()).fromString("3/9/2013", false) : 9 of march
Date.prototype.fromString = function(str, ddmmyyyy) {
    var m = str.match(/(\d+)(-|\/)(\d+)(?:-|\/)(?:(\d+)\s+(\d+):(\d+)(?::(\d+))?(?:\.(\d+))?)?/);
    if(m[2] == "/"){
        if(ddmmyyyy === false)
            return new Date(+m[4], +m[1] - 1, +m[3], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
        return new Date(+m[4], +m[3] - 1, +m[1], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
    }
    return new Date(+m[1], +m[3] - 1, +m[4], m[5] ? +m[5] : 0, m[6] ? +m[6] : 0, m[7] ? +m[7] : 0, m[8] ? +m[8] * 100 : 0);
}

这篇关于Javascript日期正则表达式DD / MM / YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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