正则表达式以匹配日期中的有效日期 [英] Regular Expression to match a valid day in a date

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

问题描述

我需要有关正则表达式的帮助,以确保用户输入有效日期 字符串的格式为mm/dd/yyyy

I need help coming up with a regex to make sure the user enters a valid date The string will be in the format of mm/dd/yyyy

这是我到目前为止提出的.

Here is what I have come up with so far.

/\[1-9]|0[1-9]|1[0-2]\/\d{1,2}\/19|20\d\d/

我已经验证了正则表达式,其中用户不能输入大于12的日期,并且年份必须以"19"或"20"开头.我遇到的麻烦是弄清楚验证日期的一些逻辑.这一天不应该超过31.

I have validated the regex where the user cannot enter a day higher than 12 and the years have to start with either "19" or "20". What I am having trouble is figuring out some logic for validating the day. The day should not go over 31.

推荐答案

0-31的正则表达式:

Regex for 0-31:

(0[1-9]|[12]\d|3[01])

或者如果您不希望日期前有零(例如05):

Or if you don't want days with a preceding zero (e.g. 05):

([1-9]|[12]\d|3[01])

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

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