Javascript 数字和破折号的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8292965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression for number and dash
提问by hungneox
Currently I write the regex like this: /^([\d+]*-)+([\d]*-)+([\d])*$/
目前我这样写正则表达式: /^([\d+]*-)+([\d]*-)+([\d])*$/
I want the result follow this pattern 00-123-456-789
or 00123456789
(dash between number group or no dash at all)
我希望结果遵循这种模式00-123-456-789
或00123456789
(数字组之间的破折号或根本没有破折号)
- not
00-123--457-789
- or
-00-123-456-789-
- or
-00123456789-
- or
00-123-456-789-
- 不是
00-123--457-789
- 或者
-00-123-456-789-
- 或者
-00123456789-
- 或者
00-123-456-789-
How can I modify the regex to matches the pattern above?
如何修改正则表达式以匹配上面的模式?
回答by jfriend00
If your question needs the specific segment lengths you specify in your examples, you can use this:
如果您的问题需要您在示例中指定的特定段长度,您可以使用:
/^\d{2}-?\d{3}-?\d{3}-?\d{3}$/
This will accept 00-123-456-789
, but will allow for any dashes to be missing. If you want to allow only for all dashes or no dashes, then you could use this:
这将接受00-123-456-789
,但将允许丢失任何破折号。如果你只想允许所有破折号或没有破折号,那么你可以使用这个:
/^\d{2}-\d{3}-\d{3}-\d{3}$|^\d{11}$/
which will accept only 00-123-456-789
or 00123456789
, but not allow only some dashes to be missing.
它将只接受00-123-456-789
or 00123456789
,但不允许只丢失一些破折号。
Or, if you meant that you could have any number of digits and any number of single dashes between them, then you could use this:
或者,如果你的意思是你可以有任意数量的数字和任意数量的单破折号,那么你可以使用这个:
/^\d+(-\d+)*$/
回答by Phil
Try something like this
尝试这样的事情
/^(\d+-?)+\d+$/
回答by MetaEd
If I understand you correctly, you wish your regex to stand for strings which consist of a number group followed (optionally) by additional number groups with a -
separator.
如果我理解正确,您希望您的正则表达式代表由数字组组成的字符串,后跟(可选)带有-
分隔符的附加数字组。
\d+ # represents a number group
(-\d+)* # represents 0 or more additional number groups beginning with "-"
So, together with the necessary beginning and end of line assertions, together we have:
所以,加上必要的行首和行尾断言,我们有:
^\d+(-\d+)*$
回答by vkuberan
The following answer will be used to match
以下答案将用于匹配
00-123-456-789 or 00 123 456 789
00-123-456-789 或 00 123 456 789
/^[\d]{2}[-\s]?[\d]{3}[-\s]?[\d]{3}[-\s]?[\d]{3}$/
/^[\d]{2}[-\s]?[\d]{3}[-\s]?[\d]{3}[-\s]?[\d]{3}$/
If you want only to match hyphen(-) or without hyphen(-)
如果您只想匹配连字符 (-) 或不匹配连字符 (-)
Then it will do
然后它会做
/^[\d]{2}[-]?[\d]{3}[-]?[\d]{3}[-]?[\d]{3}$/
/^[\d]{2}[-]?[\d]{3}[-]?[\d]{3}[-]?[\d]{3}$/
回答by Godwin
If you only want to accept ##-###-###-### or ###########, then what you need is something like:
如果您只想接受##-###-###-### 或###########,那么您需要的是:
/^(([\d+]{2}\-)([\d]{3}\-){2}([\d]{3})|[\d]{11})$/
回答by Alessandro Vendruscolo
From what have you provided us, this regex ^-?(\d+-?)+$
matches this
根据您提供给我们的信息,此正则表达式^-?(\d+-?)+$
与此匹配
-00-123-456-789-
-00123456789-
00-123-456-789-
00123456789
but doesn't match this:
但与此不符:
00-123--457-789
Breakdown:
分解:
- the string can start with a dash (
^-?
) - it has some digits followed by an optional dash (
\d+-?
) - the last group can be repeated one or more times (
(\d+-?)+
) - the string ends (
$
)
- 字符串可以以破折号 (
^-?
)开头 - 它有一些数字后跟可选的破折号 (
\d+-?
) - 最后一组可以重复一次或多次 (
(\d+-?)+
) - 字符串结束 (
$
)
回答by techno
@godwin: since I can't add a comment to your post and no one else seems to have responded, let me address at least one issue. At the beginning you have
@godwin:由于我无法在您的帖子中添加评论,而且似乎没有其他人回复,让我至少解决一个问题。一开始你有
[\d+]{2}\-
\d+
will match one or more digits and you're asking for it to be repeated twice, so the whole segment will match 123456789-
:
\d+
将匹配一位或多位数字,并且您要求将其重复两次,因此整个段将匹配123456789-
:
[\d+]
=12345678
(greedy matching)[\d+]
=9
\-
=-
[\d+]
=12345678
(贪婪匹配)[\d+]
=9
\-
=-
You also probably didn't need to escape the dash because it's not inside a group block "[]"
.
您也可能不需要逃避破折号,因为它不在组块内"[]"
。