感谢另一位出色的StackOverflow用户,我有这个正则表达式
/(?:-\d+)*/g
Run Code Online (Sandbox Code Playgroud)
我想要它匹配的东西
133-134-454-58819860
12-13-876-1234346
Run Code Online (Sandbox Code Playgroud)
每个块(-s之间的数字)可以是任何长度但它肯定只是数字而且只有4个块.
但目前它匹配像-2008这样的东西
我真的很擅长正则表达式而我正在努力,请帮助.如果有帮助,我会使用JavaScript.
/(?:-\d+)*/g
Run Code Online (Sandbox Code Playgroud)
分解为:
/ about to begin a regex
( the following is a group
?: but don't bother storing what this group finds as its own result
- it must have a dash
\d followed by digit(s)...
+ at least one digit, perhaps more
) Im done with the group
* But get me as many groups like that as you could
/ done with the regex
Run Code Online (Sandbox Code Playgroud)
所以它会找到像这样的所有组-0000但不喜欢这个-000-000
在撰写本文时,其他更快的用户发布了他们自己的正则表达式.但我仍然发布,所以你遵循逻辑.