javascript 字符串的正则表达式必须至少包含 14 个字符,其中至少 2 个是数字,至少 6 个是字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5527191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:30:08  来源:igfitidea点击:

Regular expression for a string that must contain minimum 14 characters, where at minimum 2 are numbers, and at minimum 6 are letters

phpjavascriptregex

提问by Chris

I need a regex that tests a string for a

我需要一个正则表达式来测试一个字符串

  • minimum of 14 characters - valid are A-Za-z0-9#,.-_
  • minimum of 6 letters within that 14
  • minimum of 2 numbers within that 14
  • 最少 14 个字符 - 有效的是 A-Za-z0-9#,.-_
  • 在这 14 个字母中至少有 6 个字母
  • 在这 14 个数字中最少有 2 个数字

Is there a way I can wrap this up in one regular expression (currently I have a javascript and php function that does three separate tests, one that it is 14 total, another that there is at least two numbers, and another that there is at least 6 letters.

有没有一种方法可以用一个正则表达式来包装它(目前我有一个 javascript 和 php 函数,它执行三个单独的测试,一个总共 14 个,另一个至少有两个数字,另一个在至少 6 个字母。

So the following would be valid:

所以以下内容是有效的:

  • blabla2bla2f54a (valid >14 total, with at least 6 letters, at least 2 numbers)
  • thisIsNotValidAtAll (invalid because less than 2 numbers)
  • blabla2bla2f54a(有效总数>14,至少有6个字母,至少2个数字)
  • thisIsNotValidAtAll(因为少于 2 个数字而无效)

回答by ridgerunner

Easy! First lets look at a commented version in PHP:

简单!首先让我们看一下 PHP 中的注释版本:

$re = '/# Match 14+ char password with min 2 digits and 6 letters.
    ^                       # Anchor to start of string.
    (?=(?:.*?[A-Za-z]){6})  # minimum of 6 letters.
    (?=(?:.*?[0-9]){2})     # minimum of 2 numbers.
    [A-Za-z0-9#,.\-_]{14,}  # Match minimum of 14 characters.
    $                       # Anchor to end of string.
    /x';

Here is the JavaScript version:

这是 JavaScript 版本:

var re = /^(?=(?:.*?[A-Za-z]){6})(?=(?:.*?[0-9]){2})[A-Za-z0-9#,.\-_]{14,}$/;

Addendum 20121130:

附录 20121130:

I noticed that this answer recently got an upvote. This uses a more outdated expression so I figured it was time to update it with a better one...

我注意到这个答案最近得到了赞成。这使用了一个更过时的表达方式,所以我认为是时候用更好的表达方式更新它了......

a more efficient expression:

更有效的表达:

By getting rid of the dot-star altogether and greedily applying a more precise expression, (a negated char class), an even more efficient solution results:

通过完全摆脱点星并贪婪地应用更精确的表达式(否定的字符类),得到了更有效的解决方案:

$re = '/# Match 14+ char password with min 2 digits and 6 letters.
    ^                              # Anchor to start of string.
    (?=(?:[^A-Za-z]*[A-Za-z]){6})  # minimum of 6 letters.
    (?=(?:[^0-9]*[0-9]){2})        # minimum of 2 numbers.
    [A-Za-z0-9#,.\-_]{14,}         # Match minimum of 14 characters.
    $                              # Anchor to end of string.
    /x';

Here is the new JavaScript version:

这是新的 JavaScript 版本:

var re = /^(?=(?:[^A-Za-z]*[A-Za-z]){6})(?=(?:[^0-9]*[0-9]){2})[A-Za-z0-9#,.\-_]{14,}$/;

Edit:Added #,.-_to list of valid chars.
Edit:Changed the greedy to lazy star.
Edit 20121130:Added alternate version with the lazy-dot-star replaced with a more efficient greedy application of a more precise expression.

编辑:添加#,.-_到有效字符列表中。
编辑:将贪婪改为懒惰的明星。
编辑 20121130:添加了替代版本,其中惰性点星替换为更有效的更精确表达式的贪婪应用程序。

回答by Ben

I would recommend multiple checks, writing a single regex for this would be ugly. Multiple checks also allows you to know what criteria wasn't met.

我建议进行多次检查,为此编写一个正则表达式会很丑陋。多项检查还可以让您知道哪些标准没有得到满足。

$input = 'blabla2bla2f54a';
$errors=array();
if (!preg_match('/^[A-Za-z0-9#,.\-_]*$/', $input))
    $errors[] = 'Invalid characters';
if (strlen($input) < 14)
    $errors[] = 'Not long enough';
if (strlen(preg_replace('/[^0-9]/','',$input)) < 2)
    $errors[] = 'Not enough numbers';
if (strlen(preg_replace('/[^A-Za-z]/','',$input)) < 6)
    $errors[] = 'Not enough letters';

if (count($errors) > 0) //Didn't work
{
    echo implode($errors,'<BR/>');
}

回答by Dejan Marjanovic

echo preg_match("/(?=.*[#,.-_])((?=.*\d{2,})(?=.*[a-zA-Z]{6,}).{14,})/", $string);

Output:

输出:

blabla2bla2f54a (1)
thisIsNotValidAtAll (0)

回答by buschtoens

No, that's not possible, because in fact you want to do three individual checks. You can't wrap them up in one expression that returns true or false. The problem is, that you have two equal checks that you would need to combine via an AND-operator, whicht is not possible. But what we can do is building a super-size RegExp that recognizes every possible case. But hat kind of RegExp is senseless, because it would take a long time to apply this test on your string. I would recommend you doing three separated tests:

不,这是不可能的,因为实际上您想要进行三项单独检查。您不能将它们包含在一个返回 true 或 false 的表达式中。问题是,您需要通过 AND 运算符合并两个相等的检查,这是不可能的。但是我们可以做的是构建一个超大的 RegExp,它可以识别所有可能的情况。但是这种正则表达式是没有意义的,因为在你的字符串上应用这个测试需要很长时间。我建议您进行三个独立的测试:

var result = string.replace(/[A-Za-z]{6}/, "").replace(/[0-9]{2}/, "").replace(/[A-Za-z0-9#,\.\-]{6}/, "").length == 0 ? true : false; // By shortening our sting we're saving time on later chained replacement methods