繁体   English   中英

使用正则表达式匹配包含恰好 N 个字符的字符串

[英]Match string containing exactly N characters with regex

我需要使用 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

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

利用

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

证明

解释

--------------------------------------------------------------------------------
  ^                        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)

证明

解释

--------------------------------------------------------------------------------
  ^                        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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM