gpt4 book ai didi

javascript - 正则表达式匹配第一个数字并且只允许 11

转载 作者:行者123 更新时间:2023-12-01 00:01:16 24 4
gpt4 key购买 nike

我正在尝试创建一个正则表达式,验证 11 个数字中的前 6 个数字是否采用此日期格式“yymmdd”,并验证它仅包含 11 个数字。

示例:06060612345

^((0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])(0?[0-9]|[1-9][0-9]))

最佳答案

正如评论中所述,使用正则表达式进行验证实际上不起作用。通过使用像 const match = "06060612345".match(/^(\d{2})(\d{2})(\d{2})(\d{ 之类的正则表达式,让自己更轻松5})$/)。然后使用Date.parse:

// Generates our yyyy/mm/dd date string for our argument.
const date = [20 + match[1], match[2], match[3]].join('/');

// And to check validity you can use this instead of the
// massive moment js lib as suggested in the comments.
const isValid = !isNaN(Date.parse(date));

如果需要,额外的数字位于 match[4] 中。

关于javascript - 正则表达式匹配第一个数字并且只允许 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60707204/

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