Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在字符串上使用正则表达式,例如:
1-* or *-2.
到目前为止,这是我想出的:
"/(.*\-2)||(1\-.*)/"
但它似乎不起作用 - 无论我的输入字符串如何,它每次都返回 true。
如何创建正则表达式来匹配这些字符串?
尝试这个 ...
"/(.+-\2)|(\1-.+)/"
试试这个(简单的)版本:
/(^1-|-2$)/
如果您需要更具体地匹配,[0-9]请在相应的位置添加,也可以添加另一个锚点 ( ^$)。
[0-9]
^$
/(^1-[0-9]$|^[0-9]-2$)/
试试这个,你需要添加 ^ 和 $ 来匹配整个字符串:
/^(1-0|0-2)$/
或者:
/^(1-.*|.*-2)$/
选择任何适合您的需求