IaMaCuP IaMaCuP - 1 year ago 191
Javascript Question

Javascript RegExp object working in Chrome, Firefox but not in IE 11 (Syntax error in regular expression)

I have the following regex test which is looking for dates in the format nn/nnnn and that are 01-12/20nn or 01-12/19nn - i.e. a month followed by a year that makes rough sense.

What I have is this

which works fine on Chrome and Firefox, but fails with the error
Syntax error in regular expression
on IE 11.

The regex also passes tests on www.regex101.com and performs as expected there.

I have included a full example below - I am a bit stuck because I am certain the regex is absolutely fine.

Any help would be much appreciated - what am I doing wrong?

<!DOCTYPE html>
<html lang="en">

<title>This is the title</title>

<script type="text/javascript">

var pattern = new RegExp(/^([0-1][0-9][\/]([1][9]|[2][0])[0-9][0-9])$/, 'i');

var goodInput = "12/2012";
var badInput = "1b/g212";

console.log("Good Input was NOT OK");
console.log("Good Input was OK");

console.log("Bad Input was NOT OK");
console.log("Bad Input was OK");




This is the body


Answer Source

You are creating a new RegExp from an existing regexp. This is an ES6 feature not supported in IE11. See this blog post for details.

Either of the following will work:

var pattern = /^([0-1][0-9][\/]([1][9]|[2][0])[0-9][0-9])$/i;


var pattern = new RegExp("^([0-1][0-9][\\/]([1][9]|[2][0])[0-9][0-9])$", "i");

By the way, why are you writing [1] instead of 1? Why are you writing [\/] instead of \/, or [/]?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download