gpt4 book ai didi

javascript - 正则表达式:最少 13 个字符,13 个字符内最多 3 个字符

转载 作者:行者123 更新时间:2023-12-03 06:35:48 28 4
gpt4 key购买 nike

我需要一个可以验证的表达式,如下所示:

1234-567.890-123

  • 最少 13 个数字
  • 最多 3 个特殊字符(“-”和 .)<-- 无论在何处

我的解决方案[0-9]{13,}[-\.]{0,3}无法工作,因为这仅验证:

1234567890123.-.

最佳答案

如果字符串中字符的顺序不重要,您可以使用

^(?=(?:[\d.-]){13,}$)(?=(?:\D*\d){13,})(?!(?:[^.-]*[.-]){4}).*$

正则表达式分解

^ #Starting of string
(?=(?:[\d.-]){13,}$) #Lookahead which sees that the string only contains digits, . and -
(?=(?:\D*\d){13,}) #Lookahead to match 13 digits
(?!(?:[^.-]*[.-]){4}) #Looahead so that the number of . and - are not equal to 4
.*
$

中间部分

(?= #Lookahead
(?:\D*\d) #Match all non-digits and then digit.
{13,} #Repeat this at least 13 times.In simple words, it is matching at least 13 digits.
)

关于javascript - 正则表达式:最少 13 个字符,13 个字符内最多 3 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221383/

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