34

I have the following javascript code:

    function checkLegalYear() {
        var val = "02/2010"; 

        if (val != '') {
           var regEx = new RegExp("^(0[1-9]|1[0-2])/\d{4}$", "g");

            if (regEx.test(val)) {
               //do something
            }
            else {
               //do something
            }
        }
    }

However, my regEx test always returns false for any value I pass (02/2010). Is there something wrong in my code? I've tried this code on various javascript editors online and it works fine.

mint
  • 3,341
  • 11
  • 38
  • 55

1 Answers1

98

Because you're creating your regular expression from a string, you have to double-up your backslashes:

var regEx = new RegExp("^(0[1-9]|1[0-2])/\\d{4}$", "g");

When you start with a string, you have to account for the fact that the regular expression will first be parsed as such — that is, as a JavaScript string constant. The syntax for string constants doesn't know anything about regular expressions, and it has its own uses for backslash characters. Thus by the time the parser is done with your regular expression strings, it will look a lot different than it does when you look at your source code. Your source string looks like

"^(0[1-9]|1[0-2])/\d{4}$"

but after the string parse it's

^(0[1-9]|1[0-2])/d{4}$

Note that \d is now just d.

By doubling the backslash characters, you're telling the string parser that you want single actual backslashes in the string value.

There's really no reason here not to use regular expression syntax instead:

var regEx = /^(0[1-9]|1[0-2])\/\d{4}$/g;

edit — I also notice that there's an embedded "/" character, which has to be quoted if you use regex syntax.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Works like a charm, thanks so much; will accept when time limit is up. – mint Jun 29 '11 at 13:44
  • The second solution is wrong, that / needs to be escaped. `...0-2])\/\d{4...` – epascarello Jun 29 '11 at 13:45
  • I went to fix it after I commented and saw you changed it. Just took quick. – epascarello Jun 29 '11 at 14:26
  • "There's really no reason here not to use regular expression syntax instead" What if you want to concatenate your expressions? One from a library surrounded by other things? – Christopher Smith May 18 '18 at 15:54
  • var regex1 = RegExp('^([0-2][0-9]|(3)[0-1](\/)(((0)[0-9])|((1)[0-2]))(\/)d{4}$','g'); var str1 = '16/12/2018'; console.log(regex1.test(str1)); // expected output: true When I test in regex101, 16/12/2018, returns true, but when I use it in my javascript file, ity always return false, why? – Rahul Sharma Apr 10 '19 at 10:47
  • @RahulSharma because you're making your regular expression from a *string* instead of a regular expression literal. – Pointy Apr 10 '19 at 11:56
  • @Pointy how should I handle it? I am confused. – Rahul Sharma Apr 10 '19 at 12:00
  • @RahulSharma I added some more explanatory text. – Pointy Apr 10 '19 at 12:05
  • @Pointy Yes sir. I did read. I implemented the following: var regex = /^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)d{4}$/g regex.test(’16/12/2018’) This also returns false every time. This regex tries to test if date is of dd/mm/yyyy format – Rahul Sharma Apr 10 '19 at 12:15
  • @RahulSharma at the end of your regex, `d{4}` should be `\d{4}`. Also you *probably* don't need the "g" flag. – Pointy Apr 10 '19 at 12:16
  • Awesome. Thanks @Pointy. – Rahul Sharma Apr 10 '19 at 12:49
  • Thanks a ton! never would've guessed this. Was plagued by this issue for an hour aahhhh – Nathan Martin Nov 20 '21 at 00:30
  • 1
    Thanks, just wasted 30 minutes on this.. – nlhnt Mar 14 '23 at 22:21
  • shouldnt it be a bug that in string it doesnt recognize the escape sequence for \s but for \\ its okay – MinhajulAnwar Apr 05 '23 at 19:22
  • @MinhajulAnwar well JavaScript string constant syntax *does* "recognize" `\s`, but it doesn't mean anything special so it treats it as "s". – Pointy Apr 05 '23 at 19:26