我对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中运行的方式重写此表达式,或者是否还有其他方法可以实现此目的?
答案 0 :(得分:1)
您可以改用正向前瞻
\w+(?:'\w+)*|[!?.](?![!?.])
var sentence = "Exclamation! Question? Full stop. Ellipsis...";
var words = sentence.toLowerCase().match(/\w+(?:'\w+)*|[!?.](?![!?.])/g);
console.log(words);