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

查看:51
本文介绍了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|)dd

我也不知道如何转义斜线,我尝试查看"字符串中的逻辑,但这就像为我查看"矩阵代码一样.我将正则表达式字符串放在 options .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|)dd,
                    "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

非常感谢

推荐答案

您可以使用验证 YYYY/MM/DD 的正则表达式并将其翻转以获得 DD/MM/YYYY 所需的内容:

You could take the regex that validates YYYY/MM/DD and flip it around to get what you need for DD/MM/YYYY:

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

顺便说一句 - 此正则表达式验证 DD/MM/YYYY 或 DD-MM-YYYY

BTW - this regex validates for either DD/MM/YYYY or DD-MM-YYYY

附言这将允许诸如 31/02/4899 之类的日期

P.S. This will allow dates such as 31/02/4899

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

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