【问题标题】:Match string containing exactly N characters with regex使用正则表达式匹配包含恰好 N 个字符的字符串
【发布时间】:2021-08-08 09:20:42
【问题描述】:

我需要使用 JavaScript 验证字符串。规则是至少一个数字,至少 1 个字母,并且限制为 10 个字符。对于少于 10 个字符的字符串,一切正常 = 返回 false,但如果我有超过 10 个字符,则返回 true,女巫不正确。

var secretS = '123456789aa';

我尝试验证

/(?=[a-zA-Z0-9]*[a-zA-Z])[a-zA-Z0-9]{10}$/.test(secretS)

如何限制10个字符,超过10个的字符串需要返回false?

更新:

如何为 9 个字母数字字符、后跟连字符和 5 个字母数字字符构建正则表达式验证。有效字符串类似于 WE17CLDEC-J6557

【问题讨论】:

  • 抱歉,我将删除第二个示例,但它的主题相同 - 正则表达式验证
  • 试试:/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{10}$/
  • @anubhava 非常感谢。这是一个很好的答案。请如果你得到 min 帮助我为 9 个字母数字字符构建正则表达式验证,后跟一个连字符,然后是 5 个字母数字字符。有效字符串类似于 WE17CLDEC-J6557
  • @anubhava 我更新了这个问题。谢谢
  • /^[a-zA-Z\d]{9}-[a-zA-Z\d]{5}$/ 应该适用于第二个要求

标签: javascript regex validation


【解决方案1】:

规则是至少一个数字,至少 1 个字母,并且限制为 10 个字符。

使用

/^(?=\d*[A-Za-z])(?=[A-Za-z]*\d)[A-Za-z\d]{10}$/.test(secretS)

proof

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    \d*                      digits (0-9) (0 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    [A-Za-z]                 any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [A-Za-z]*                any character of: 'A' to 'Z', 'a' to 'z'
                             (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [A-Za-z\d]{10}           any character of: 'A' to 'Z', 'a' to 'z',
                           digits (0-9) (10 times)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

9 个字母数字字符,后跟一个连字符,然后是 5 个字母数字字符

使用

/^[a-zA-Z\d]{9}-[a-zA-Z\d]{5}$/.test(secretS)

proof

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [a-zA-Z\d]{9}            any character of: 'a' to 'z', 'A' to 'Z',
                           digits (0-9) (9 times)
--------------------------------------------------------------------------------
  -                        '-'
--------------------------------------------------------------------------------
  [a-zA-Z\d]{5}            any character of: 'a' to 'z', 'A' to 'Z',
                           digits (0-9) (5 times)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

    【解决方案2】:

    var secretS = '123456789aa';
    console.log (/^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{10}$/.test(secretS));

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 2016-04-21
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2022-11-22
      • 1970-01-01
      相关资源
      最近更新 更多