3 回答
TA贡献2016条经验 获得超9个赞
您最好进行分割-并测试所有元素。但是,如果您真的想使用正则表达式,可以尝试以下一种:
/^(?:(?:31-(?:(?:0?[13578])|(1[02]))-(19|20)?\d\d)|(?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)|(?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))|(?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d))$/
说明:
^ # start of line
(?: # group without capture
# that match 31st of month 1,3,5,7,8,10,12
(?: # group without capture
31 # number 31
- # dash
(?: # group without capture
(?: # group without capture
0? # number 0 optionnal
[13578] # one digit either 1,3,5,7 or 8
) # end group
| # alternative
(1[02]) # 1 followed by 0,1 or 2
) # end group
- # dash
(19|20)? #numbers 19 or 20 optionnal
\d\d # 2 digits from 00 to 99
) # end group
|
(?:(?:29|30)-(?:(?:0?[13-9])|(?:1[0-2]))-(?:19|20)?\d\d)
|
(?:29-0?2-(?:19|20)(?:(?:[02468][048])|(?:[13579][26])))
|
(?:(?:(?:0?[1-9])|(?:1\d)|(?:2[0-8]))-(?:(?:0?[1-9])|(?:1[0-2]))-(?:19|20)?\d\d)
)
$
我已经解释了第一部分,其余部分留作练习。
此匹配一个无效日期:29-02-1900但对于介于01-01-1900和之间的任何日期都是正确的31-12-2099
添加回答
举报