简体   繁体   English

regex:字符串中的最小位数

[英]regex : minimum total number of digits in a string

I'm trying to know how many digits there is in a string that is essentially like a password. 我想知道一个本质上像密码的字符串中有多少个数字。

For now I have this regex : 现在我有这个正则表达式:

^(?=.*[0-9]{3,})([a-zA-Z0-9_/+*.-]{6,})$

It works great when their is 3 digits in a row but not when they are separated in the whole string. 当它们连续为3位数字时,效果很好,但是当它们在整个字符串中分开时,效果不佳。

I need to be able to know if there is 3 digit in strings like those : 我需要知道字符串中是否有3位数字:

h123dasd 1hkh/23jd 1gvbn/*2fefse- h123dasd 1hkh / 23jd 1gvbn / * 2fefse-

What can I do ? 我能做什么 ?

You can use this regex: 您可以使用此正则表达式:

/^(?=(?:\D*\d){3,})[a-zA-Z0-9_/+*.-]{6,}$/

This will enforce 3 digits in your input that may or may not be consecutive. 这将在您的输入中强制输入3位数字,该数字可以连续也可以不连续。

RegEx Demo 正则演示

No need for such a complicated regex IMO - just extract digits from the string, concat the matches, and then check the length. 不需要这么复杂的正则表达式IMO-只需从字符串中提取数字,合并匹配项,然后检查长度即可。 Something like: 就像是:

str.match(/\d+/g).reduce((p, c) => p + c).length > 3;

DEMO 演示

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

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