表示字符串中至少有10个数字字符的正则表达式。
可能有10人以上,但不少于10人。在任意位置可以有任意数量的其他字符,将数字分隔开来。
示例数据:
(123) 456-7890
123-456-7890 ext 41
1234567890
etc.发布于 2014-02-12 16:45:14
要确保至少有10位数字存在,请使用以下正则表达式:
/^(\D*\d){10}/代码:
var valid = /^(\D*\d){10}/.test(str);测试:
console.log(/^(\D*\d){10}/.test('123-456-7890 ext 41')); // true
console.log(/^(\D*\d){10}/.test('123-456-789')); // false解释:
^ assert position at start of the string
1st Capturing group (\D*\d){10}
Quantifier: Exactly 10 times
Note: A repeated capturing group will only capture the last iteration.
Put a capturing group around the repeated group to capture all iterations or use a
non-capturing group instead if you're not interested in the data
\D* match any character that's not a digit [^0-9]
Quantifier: Between zero and unlimited times, as many times as possible
\d match a digit [0-9]发布于 2014-02-12 16:42:02
最简单的方法可能是去掉所有的非数字字符,然后数一数剩下的:
var valid = input.replace(/[^\d]/g, '').length >= 10注:.replace不修改原始字符串。
发布于 2014-02-12 16:48:12
(\d\D*){10}一个数字,后面跟着任意数量的非数字,十次。
https://stackoverflow.com/questions/21733994
复制相似问题