正则表达式日期DD / MM / YYYY

时间:2020-07-17 19:46:20

标签: javascript regex

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

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

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

1 个答案:

答案 0 :(得分:2)

参考: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)