gpt4 book ai didi

JavaScript `match` 行为

转载 作者:行者123 更新时间:2023-11-28 16:07:51 25 4
gpt4 key购买 nike

在我的 Chrome 控制台中,我尝试了以下方法:

>  "abcd".match(/(.+)+/)
< ["abcd", "abcd"]

match 返回两个“结果”这一事实对我来说是出乎意料且奇怪的。我在 Firefox 中进行了测试,结果是相同的(所以我认为这不是任何类型的错误)。

但是,在 Python 中:

>  re.findall(r'(.+)+', 'abba')
< ['abba']

我无法解释这一点。这是怎么回事?

最佳答案

根据MDN :

If the regular expression does not include the g flag, returns the same result as regexp.exec(string).

来自 exec :

The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.

因此,返回数组中的第一项是匹配项 ( "abcd" ),第二项是唯一的括号组(恰好也是 "abcd" )。

关于JavaScript `match` 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14145206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com