gpt4 book ai didi

javascript - Firefox 中的正则表达式组(反向前瞻)无效

转载 作者:行者123 更新时间:2023-12-02 23:00:44 25 4
gpt4 key购买 nike

我有一个相当困惑的正则表达式 match words and punctuation within a sentence :

var sentence = "Exclamation! Question? Full stop. Ellipsis...";
var words = sentence.toLowerCase().match(/\w+(?:'\w+)*|(?<![!?.])[!?.]/g);
console.log(words);

在 Chrome 中,输出:

[ "exclamation", "!", "question", "?", "full", "stop", ".", "ellipsis", "." ]

在 Firefox 中,此表达式会导致错误,这似乎是由于反向先行造成的。

我想知道是否可以以适用于 Firefox 的方式重写此表达式,或者是否有任何其他方式可以实现此目的?

最佳答案

您可以使用正向前瞻

\w+(?:'\w+)*|[!?.](?![!?.])

var sentence = "Exclamation! Question? Full stop. Ellipsis...";
var words = sentence.toLowerCase().match(/\w+(?:'\w+)*|[!?.](?![!?.])/g);
console.log(words);

关于javascript - Firefox 中的正则表达式组(反向前瞻)无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57803871/

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