0

I have a rather confusing regex expression to 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);

In Chrome, this outputs:

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

In Firefox, this expression causes an error which seems to be due to the reverse lookahead.

I was wondering if it would be possible to rewrite this expression in a way which will work in Firefox, or if there is any other way in which I could accomplish this?

MysteryPancake
  • 1,235
  • 1
  • 15
  • 42

1 Answers1

1

You can use a positive lookahead instead

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

var sentence = "Exclamation! Question? Full stop. Ellipsis...";
var words = sentence.toLowerCase().match(/\w+(?:'\w+)*|[!?.](?![!?.])/g);
console.log(words);
Code Maniac
  • 35,187
  • 4
  • 31
  • 54