javascript - 正则表达式日期 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)
解决方案
参考: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)
推荐阅读
- macos - 在 macOS 中安装 kubeadm
- java - 如何在按下按钮时动态更改图片并保存该图像的状态
- json - 如何将 spaCy 自定义 Span 扩展序列化为 JSON?
- java - assertEquals JUnit 方法参数倒置有很大风险吗?
- c++ - 如何在动态库 (/MD) 项目中使用静态库 (/MT)?
- c++ - 重用与项目关联的小部件时 QListWidget 崩溃
- java - 为什么 JButton btn[3][5] 没有被放置在 setBounds() 调用中给定的位置上?
- java - Lambda 表达式 Map.computeIfPresent() 和 Map.merge()
- python - 如何编写从文件中获取端点返回的数据乘以查询中的参数
- vue.js - 当组件数据加载到vue js中时如何触发组件转换?