简体   繁体   中英

why does “aabbcc”[/ab*/] only return “a”?

如果量词*表示“零次或多次”,则似乎"aabbcc"[/ab*/]应返回"abb"但仅返回"a"

The regex returns exactly what you asked : the first occurence of 1 a followed by 0 or more b .

If you want all the non-overlapping occurences, you could use scan :

"aabbcc".scan(/ab*/)
#=> ["a", "abb"]

If you want to have a least 1 b , you could use b+ :

"aabbcc"[/ab+/]
#=> "abb"

好的,所以我才知道这是因为最左边的比赛获胜并且因为在abb之前有a匹配的a才返回。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM