The following regex will match the range 9-11 digits: /\d{9,11}/
以下正则表达式将匹配范围9-11位数:/ \ d {9,11} /
What is the best way to write a regex matching exactly 9 or 11 digits (excluding 10)?
编写正好匹配9或11位数(不包括10位)的正则表达式的最佳方法是什么?
Using the pattern attribute of an input element, thus the regex should match the entire value of the input field. I want to accept any number containing 9 or 11 digits.
使用输入元素的pattern属性,因此正则表达式应匹配输入字段的整个值。我想接受包含9或11位数的任何数字。
3 个解决方案
#1
20
Well, you could try something like:
好吧,你可以尝试类似的东西:
^\d{9}(\d{2})?$
This matches exactly nine digits followed by an optional extra-two-digits (i.e., 9 or 11 digits).
这恰好匹配九位数字,后跟可选的两位数字(即9或11位数字)。
Alternatively,
或者,
^(\d{9}|\d{11})$
may work as well.
也可以。
But remember that not everything necessarily has to be done with regular expressions. It may be just as easy to check the string matches ^\d*$
and that the string length itself is either 9 or 11 (using something like strlen
, for example).
但请记住,并非所有事情都必须使用正则表达式完成。检查字符串匹配^ \ d * $并且字符串长度本身是9或11(例如使用strlen之类的东西)可能同样容易。
#2
3
This regex would do
这个正则表达式会这样做
^(\d{9}|\d{11})$
or if you dont want to match it exactly
或者如果你不想完全匹配它
\D(\d{9}|\d{11})\D
#3
2
/[^\d](\d{9}|\d{11})[^\d]/
Depending on which tool you are using, you may need to escape the (, | and ) characters.
根据您使用的工具,您可能需要转义(,|和)字符。
Note that in order to not match 8, or any other number other than 9 or 11, the regex must be bounded with something to indicate that the match is surrounded by non-digit characters. Ideally, it would be some kind of word-boundary character, but the syntax for that would vary depending on the tool.
请注意,为了不匹配8或除9或11以外的任何其他数字,正则表达式必须与某些东西绑定,以指示匹配由非数字字符包围。理想情况下,它会是某种字边界字符,但其语法会因工具而异。
/\b(\d{9}|\d{11})\b/
works with some tools. What are you working with?
适用于某些工具。你在忙什么?
#1
20
Well, you could try something like:
好吧,你可以尝试类似的东西:
^\d{9}(\d{2})?$
This matches exactly nine digits followed by an optional extra-two-digits (i.e., 9 or 11 digits).
这恰好匹配九位数字,后跟可选的两位数字(即9或11位数字)。
Alternatively,
或者,
^(\d{9}|\d{11})$
may work as well.
也可以。
But remember that not everything necessarily has to be done with regular expressions. It may be just as easy to check the string matches ^\d*$
and that the string length itself is either 9 or 11 (using something like strlen
, for example).
但请记住,并非所有事情都必须使用正则表达式完成。检查字符串匹配^ \ d * $并且字符串长度本身是9或11(例如使用strlen之类的东西)可能同样容易。
#2
3
This regex would do
这个正则表达式会这样做
^(\d{9}|\d{11})$
or if you dont want to match it exactly
或者如果你不想完全匹配它
\D(\d{9}|\d{11})\D
#3
2
/[^\d](\d{9}|\d{11})[^\d]/
Depending on which tool you are using, you may need to escape the (, | and ) characters.
根据您使用的工具,您可能需要转义(,|和)字符。
Note that in order to not match 8, or any other number other than 9 or 11, the regex must be bounded with something to indicate that the match is surrounded by non-digit characters. Ideally, it would be some kind of word-boundary character, but the syntax for that would vary depending on the tool.
请注意,为了不匹配8或除9或11以外的任何其他数字,正则表达式必须与某些东西绑定,以指示匹配由非数字字符包围。理想情况下,它会是某种字边界字符,但其语法会因工具而异。
/\b(\d{9}|\d{11})\b/
works with some tools. What are you working with?
适用于某些工具。你在忙什么?