Reputation: 845
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 /^([0-1][0-9][\/]([1][9]|[2][0])[0-9][0-9])$/
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">
<head>
<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";
if(!pattern.test(goodInput))
{
console.log("Good Input was NOT OK");
}
else
{
console.log("Good Input was OK");
}
if(!pattern.test(badInput))
{
console.log("Bad Input was NOT OK");
}
else
{
console.log("Bad Input was OK");
}
</script>
</head>
<body>
This is the body
</body>
</html>
Upvotes: 0
Views: 860
Reputation:
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. Browser compatibility details for this usage can be found here. It is described in the spec; note the patternIsRegexp case.
Either of the following will work:
var pattern = /^([0-1][0-9][\/]([1][9]|[2][0])[0-9][0-9])$/i;
or
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 \/
?
Upvotes: 2