1

I have a regular expression as following :

var re = new RegExp('(?<!\r)\n', 'g');

which works fine in Chrome, but gets following error in Firefox :
SyntaxError: invalid regexp group

It also works in node.js

ianhoo
  • 11
  • 3
  • 2
    You are using a negative lookbehind which is a new(ish) feature and not supported by all browsers: https://stackoverflow.com/questions/50011366/javascript-regex-negative-lookbehind-not-working-in-firefox – PaulS Jan 30 '20 at 18:39

1 Answers1

0

You could use a try/catch expression

try {
    var re = new RegExp('(?<!\r)\n', 'g');
}
catch() {
    var firefox = true;
    //add alternate RegExp
}

you could also check its useragent:

if (navigator.userAgent.indexOf("Chrome") !== -1) {
    //Code that works on chrome
} else {
    //code for firefox
}

this is because chrome supports look-behind expressions, while firefox doesn't.

source: https://www.w3schools.com/ and https://developer.mozilla.org/

Julian
  • 54
  • 4