简体   繁体   English

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

[英]Match string containing exactly N characters with regex

I need to validate a string using JavaScript.我需要使用 JavaScript 验证字符串。 The rules are at least one number, at least 1 letter, and a limit to 10 characters.规则是至少一个数字,至少 1 个字母,并且限制为 10 个字符。 All works fine for a string with less than 10 characters = return false but if I have more than 10 characters, it returns true witch is not right.对于少于 10 个字符的字符串,一切正常 = 返回 false,但如果我有超过 10 个字符,则返回 true,女巫不正确。

var secretS = '123456789aa';

and I try to validate with我尝试验证

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

How to limit to 10 characters so string with more than 10 needs to return false?如何限制为 10 个字符以便超过 10 个的字符串需要返回 false?

UPDATE:更新:

How to build regex validation for 9 alphanumerical characters, followed by a hyphen, followed by 5 alphanumerical characters.如何为 9 个字母数字字符、后跟连字符和 5 个字母数字字符构建正则表达式验证。 The valid string will be like WE17CLDEC-J6557有效字符串类似于 WE17CLDEC-J6557

The rules are at least one number, at least 1 letter, and a limit to 10 characters.规则是至少一个数字,至少 1 个字母,并且限制为 10 个字符。

Use利用

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

See proof .证明

EXPLANATION解释

--------------------------------------------------------------------------------
  ^                        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 alphanumerical characters, followed by a hyphen, followed by 5 alphanumerical characters 9 个字母数字字符,后跟一个连字符,然后是 5 个字母数字字符

Use利用

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

See proof .证明

EXPLANATION解释

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