首页 > 解决方案 > 正则表达式日期 DD/MM/YYYY

问题描述

有人可以解释一下这个正则表达式的所有组件是如何工作的吗?

const carDateRegex = /^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/

将查找格式为:DD/MM/YYYY 的日期(例如:31/12/2019)

标签: javascriptregex

解决方案


参考:Regex101.com ^([0-2][0-9]|(3)[0-1])(/)(((0)[0-9])|((1)[0-2 ]))(/)\d{4}$

/
^ asserts position at start of the string
1st Capturing Group ([0-2][0-9]|(3)[0-1])
1st Alternative [0-2][0-9]
Match a single character present in the list below [0-2]
0-2 a single character in the range between 0 (index 48) and 2 (index 50) (case sensitive)
Match a single character present in the list below [0-9]
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative (3)[0-1]
2nd Capturing Group (3)
3 matches the character 3 literally (case sensitive)
Match a single character present in the list below [0-1]
0-1 a single character in the range between 0 (index 48) and 1 (index 49) (case sensitive)
3rd Capturing Group (\/)
\/ matches the character / literally (case sensitive)
4th Capturing Group (((0)[0-9])|((1)[0-2]))
1st Alternative ((0)[0-9])
5th Capturing Group ((0)[0-9])
6th Capturing Group (0)
0 matches the character 0 literally (case sensitive)
Match a single character present in the list below [0-9]
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative ((1)[0-2])
7th Capturing Group ((1)[0-2])
8th Capturing Group (1)
1 matches the character 1 literally (case sensitive)
Match a single character present in the list below [0-2]
9th Capturing Group (\/)
\/ matches the character / literally (case sensitive)
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

推荐阅读