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.
/.*?/.exec("abc");//output [""]
我认为.*?是非贪婪的,它应该返回a
.*?
a
好吧,这是意料之中的,因为.*意味着 0 或更多,并且通过将?其设置为非贪婪,因此它匹配一个空字符串。
.*
?
如果你想匹配,a那么你应该使用:
/.+?/.exec("abc");
Difference+代替*which 意味着使用非贪婪量词匹配 1 个或多个字符。
+
*
通过使用*而不是例如+,您允许将空字符串匹配为非贪婪选项。