1

我被正则表达式卡住了,只能验证 1-10 之前可能有两个破折号(连字符)的数字,例如:

--9

或者

--10

或者

--1

但不是

--11 or not --0

在我看来,我尝试了一切,例如:

/(-\-\[1-10])/

怎么了?

编辑1:

非常感谢这么多工作示例!

如果我还想在所有这些之前验证数字怎么办,例如:

8--10 but not 0--10 or not 11--11

我试过了,但没有用:

/--([1-9]|10:[1-9]|10)\b/

编辑2:

哦,这个终于奏效了:

/^(10|[1-9])--(10|[1-9])$/
4

5 回答 5

3

试一试:

/\b(?:[1-9]|10)--(?:[1-9]|10)\b/

根据OP的编辑进行更改。

解释:

The regular expression:

(?-imsx:\b(?:[1-9]|10)--(?:[1-9]|10)\b)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [1-9]                    any character of: '1' to '9'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    10                       '10'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  --                       '--'
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [1-9]                    any character of: '1' to '9'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    10                       '10'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-08-27T13:33:41.283 回答
2

The correct regex is

/\b--([1-9]|10)\b/

You're incorrectly escaping the first [ of your character class as \[. The character class used is incorrect as well. It would be treated as a character class with members 1 to 1 and a 0 i.e. [10] which means it matches either 0 or 1.

Also, the hyphens - don't need to be escaped outside a character class []. To validate the numbers that come before the hyphens as well use

/\b([1-9]|10)--([1-9]|10)\b/
于 2013-08-27T13:34:40.023 回答
2

在字符类之外,您不需要转义连字符。此外,您的角色类[1-10]只会匹配1and 0,因为[1-10]等于[10]and 只会匹配1and 0。试试这个正则表达式:

/^--(10|[1-9])$/
于 2013-08-27T13:37:05.110 回答
2

我想这会适合

/\-\-([1-9]|10)\b/

如果您不想捕获您的号码,请添加?:

/\-\-(?:[1-9]|10)\b/
于 2013-08-27T13:29:37.823 回答
1

当你写[1-10]时,它表示字符 1 到 1 + 0 字符。就好像你写了[0-1]。

实际上,在您的情况下,最好分别测试用例--1 到--9 和--10,例如:/^(--10)|(--[1-9])$/

您可以在http://myregexp.com/上测试您的正则表达式

于 2013-08-27T13:45:15.493 回答