至少x个数字字符的javascript正则表达式
用于显示字符串中至少有10个数字字符的正则表达式 可以超过10个,但不能少于10个。 在任意位置可以有任意数量的其他字符,用于分隔数字 示例数据:至少x个数字字符的javascript正则表达式,javascript,regex,Javascript,Regex,用于显示字符串中至少有10个数字字符的正则表达式 可以超过10个,但不能少于10个。 在任意位置可以有任意数量的其他字符,用于分隔数字 示例数据: (123) 456-7890 123-456-7890 ext 41 1234567890 etc. 可能最简单的方法是去掉所有非数字字符,然后计算剩余的字符数: var valid = input.replace(/[^\d]/g, '').length >= 10 注意:.replace不会修改原始字符串。可能最简单的方法是去掉所有非数
(123) 456-7890
123-456-7890 ext 41
1234567890
etc.
可能最简单的方法是去掉所有非数字字符,然后计算剩余的字符数:
var valid = input.replace(/[^\d]/g, '').length >= 10
注意:
.replace
不会修改原始字符串。可能最简单的方法是去掉所有非数字字符并计算剩余的字符:
var valid = input.replace(/[^\d]/g, '').length >= 10
注意:
.replace
不会修改原始字符串。为确保至少有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]
测试:
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]
说明:
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]
为确保至少有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]
测试:
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]
说明:
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]
一个数字后跟任意数量的非数字,十次
一个数字后跟任意数量的非数字,十次。我讨厌正则表达式。尽管我已经使用它们多少年了,但它的工作原理并不是100%显而易见。我补充了一些解释。我讨厌正则表达式。尽管我已经用了多少年,谁也不知道它是如何工作的,但现在还不是100%清楚。我补充了一些解释。