Tim*_*ker 47
有一种方法:
^(?=[0-9]*$)(?:.{8}|.{11})$
Run Code Online (Sandbox Code Playgroud)
或者,如果你想先做长度检查,
^(?=(?:.{8}|.{11})$)[0-9]*$
Run Code Online (Sandbox Code Playgroud)
这样,你只有一次复杂的部分和.
长度检查的通用.
说明:
^ # Start of string
(?= # Assert that the following regex can be matched here:
[0-9]* # any number of digits (and nothing but digits)
$ # until end of string
) # (End of lookahead)
(?: # Match either
.{8} # 8 characters
| # or
.{11} # 11 characters
) # (End of alternation)
$ # End of string
Run Code Online (Sandbox Code Playgroud)