普通防爆pression验证数字与一个空格和一个可选的特殊字符可选、空格、特殊字符、普通

2023-09-04 00:21:11 作者:我为你丶倾尽所有

有人可以告诉我如何验证数字序列与一个空格,并在年底会有一个可选的特殊字符说'#',再其次是一些5位。

Can someone tell me how to validate sequence of numbers with one space and at the end there will be a optional special character say '#' and again followed by some 5 digits.

例如:

12345 6587#2584

12345 6587#2584

我曾尝试与

(0(?:\d{0,11}|(?=\d* \d*$)[\d ]{0,12}))

但我不知道如何添加到底后面跟着数字可选的'#'。

but I don't know how to add optional '#' in the end followed by digits.

推荐答案

这应该做的伎俩

/^\d+\s\d+(?:#\d+)?$/

看到它在 rubular

^      beginning of string
\d+    one or more numbers
\s     any whitespace character
\d+    one or more numbers
(?:    begin non-capturing group
  #    literal '#' character
  \d+  one or more numbers
)      end non-capturing group
$      end of string

修改

/^0[\d\s]{,11}(?:#\d{,5}?$/

比赛开始 0 字符串,然后是一个最大的11个数字或空格。其次是一个可选的用最大的5个号码后。

Matches a string starting with 0, followed by a max of 11 numbers or spaces. Followed by an optional # with a max of 5 numbers after it.