gpt4 book ai didi

javascript - 如何将具有 mm/dd/yyyy 格式的日期与分隔各部分的特定符号匹配?

转载 作者:行者123 更新时间:2023-11-30 09:48:03 24 4
gpt4 key购买 nike

所以我知道如何让它在某个地方接受任何符号,但我将如何使用正则表达式来匹配这样的日期:

12-12-1212
12.12.1212
12/12/1212

但不匹配使用任何其他符号的日期:

12~12~1212
12=12=1212

最佳答案

描述

(?:0[1-9]|1[012])([-.\/])(?:0[1-9]|[1-2][0-9]|3[01])\1(?:[0-9]{4})

Regular expression visualization

此正则表达式将执行以下操作:

  • 匹配格式类似于日期的子字符串 mm-dd-yyyy
  • 允许分隔符为-./
  • 要求两个分隔符相同
  • 验证月份仅在 01-12 范围内
  • 验证日期只能在 01-31 范围内

例子

现场演示

https://regex101.com/r/mT0kE8/2

示例文本

mm-dd-yyyy

12-12-1212
12.12.1212
12/12/1212
But NOT match to dates using any other symbols like this:

12~12~1212
12=12=1212

样本匹配

  • 捕获组 0 获取整个日期
  • 捕获组 1 获取分隔符以确保它们相同
[0][0] = 12-12-1212
[0][1] = -

[1][0] = 12.12.1212
[1][1] = .

[2][0] = 12/12/1212
[2][1] = /

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
0 '0'
----------------------------------------------------------------------
[1-9] any character of: '1' to '9'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
1 '1'
----------------------------------------------------------------------
[012] any character of: '0', '1', '2'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[-.\/] any character of: '-', '.', '\/'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
0 '0'
----------------------------------------------------------------------
[1-9] any character of: '1' to '9'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[1-2] any character of: '1' to '2'
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
3 '3'
----------------------------------------------------------------------
[01] any character of: '0', '1'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------

关于javascript - 如何将具有 mm/dd/yyyy 格式的日期与分隔各部分的特定符号匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com