[英]Regex to check for total number of digits with a min and max
I want to test a string of all alphanumeric characters and return true only if the total number of digits in the string is between 7 and 11.我想测试一个包含所有字母数字字符的字符串,并且仅当字符串中的总位数在 7 到 11 之间时才返回 true。
1234567 // true
1234567 // 真
11qqw3qd1221wqd2132 // true
11qqw3qd1221wqd2132 // 真
abc123def456ghi789klm012 // false
abc123def456ghi789klm012 // 假
The min 7 and max 11 only applies to the digits within the string.最小 7 和最大 11 仅适用于字符串中的数字。
So I know how to test a string of digits for a min and max length:所以我知道如何测试一串数字的最小和最大长度:
^[\d]{7,11}$
But how do i do the same when :但我如何做同样的事情:
A) the digits dont have to be consecutive and A) 数字不必是连续的,并且
B) there are other characters or white spaces in the string. B) 字符串中有其他字符或空格。
Try this尝试这个
var str = "...";
var digits = str.length - str.replace(/[0-9]/g, '').length;
var ok = digits >=7 && digits <=11;
Or this或这个
str.match(/^([^0-9]*[0-9]){7,11}[^0-9]*$/)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.