Why do regex constructors need to be double escaped?












27















In the regex below, s denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.



But this is not the case as double escapes are required.



Why is this?



var res = new RegExp('(\s|^)' + foo).test(moo);


Is there a concrete example of how a single escape could be mis-interpreted as something else?










share|improve this question

























  • Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).

    – GJK
    Jul 25 '13 at 16:04






  • 2





    To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect: /(s|^)/

    – Dan Tao
    Jul 25 '13 at 16:05











  • Related: stackoverflow.com/a/37329801/1225328.

    – sp00m
    Jul 30 '18 at 9:00
















27















In the regex below, s denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.



But this is not the case as double escapes are required.



Why is this?



var res = new RegExp('(\s|^)' + foo).test(moo);


Is there a concrete example of how a single escape could be mis-interpreted as something else?










share|improve this question

























  • Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).

    – GJK
    Jul 25 '13 at 16:04






  • 2





    To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect: /(s|^)/

    – Dan Tao
    Jul 25 '13 at 16:05











  • Related: stackoverflow.com/a/37329801/1225328.

    – sp00m
    Jul 30 '18 at 9:00














27












27








27


9






In the regex below, s denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.



But this is not the case as double escapes are required.



Why is this?



var res = new RegExp('(\s|^)' + foo).test(moo);


Is there a concrete example of how a single escape could be mis-interpreted as something else?










share|improve this question
















In the regex below, s denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.



But this is not the case as double escapes are required.



Why is this?



var res = new RegExp('(\s|^)' + foo).test(moo);


Is there a concrete example of how a single escape could be mis-interpreted as something else?







javascript regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 at 9:35









CertainPerformance

92k165382




92k165382










asked Jul 25 '13 at 15:58









SmurfetteSmurfette

5601615




5601615













  • Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).

    – GJK
    Jul 25 '13 at 16:04






  • 2





    To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect: /(s|^)/

    – Dan Tao
    Jul 25 '13 at 16:05











  • Related: stackoverflow.com/a/37329801/1225328.

    – sp00m
    Jul 30 '18 at 9:00



















  • Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).

    – GJK
    Jul 25 '13 at 16:04






  • 2





    To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect: /(s|^)/

    – Dan Tao
    Jul 25 '13 at 16:05











  • Related: stackoverflow.com/a/37329801/1225328.

    – sp00m
    Jul 30 '18 at 9:00

















Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).

– GJK
Jul 25 '13 at 16:04





Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).

– GJK
Jul 25 '13 at 16:04




2




2





To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect: /(s|^)/

– Dan Tao
Jul 25 '13 at 16:05





To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect: /(s|^)/

– Dan Tao
Jul 25 '13 at 16:05













Related: stackoverflow.com/a/37329801/1225328.

– sp00m
Jul 30 '18 at 9:00





Related: stackoverflow.com/a/37329801/1225328.

– sp00m
Jul 30 '18 at 9:00












4 Answers
4






active

oldest

votes


















29














You are constructing the regular expression by passing a string to the RegExp constructor.



You need to escape the so that your string literal can express it as data before you transform it into a regular expression.






share|improve this answer
























  • That pertains to both regular string literals as well as template string literals.

    – Wiktor Stribiżew
    Sep 27 '18 at 15:28



















13














Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t, n, ", etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.



So when you generate a string by saying var someString = '(\s|^)', what you're really doing is creating an actual string with the value (s|^).






share|improve this answer































    4














    is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .



    EDIT: Even had to do it here, because \ in my answer turned to .






    share|improve this answer

































      4














      The Regex needs a string representation of s, which in JavaScript can be produced using the literal "\s".



      Here's a live example to illustrate why "s" is not enough:






      alert("One backslash:          snDouble backslashes: \s");





      Note how an extra before s changes the output.






      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17863066%2fwhy-do-regex-constructors-need-to-be-double-escaped%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        29














        You are constructing the regular expression by passing a string to the RegExp constructor.



        You need to escape the so that your string literal can express it as data before you transform it into a regular expression.






        share|improve this answer
























        • That pertains to both regular string literals as well as template string literals.

          – Wiktor Stribiżew
          Sep 27 '18 at 15:28
















        29














        You are constructing the regular expression by passing a string to the RegExp constructor.



        You need to escape the so that your string literal can express it as data before you transform it into a regular expression.






        share|improve this answer
























        • That pertains to both regular string literals as well as template string literals.

          – Wiktor Stribiżew
          Sep 27 '18 at 15:28














        29












        29








        29







        You are constructing the regular expression by passing a string to the RegExp constructor.



        You need to escape the so that your string literal can express it as data before you transform it into a regular expression.






        share|improve this answer













        You are constructing the regular expression by passing a string to the RegExp constructor.



        You need to escape the so that your string literal can express it as data before you transform it into a regular expression.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 25 '13 at 16:03









        QuentinQuentin

        652k728861049




        652k728861049













        • That pertains to both regular string literals as well as template string literals.

          – Wiktor Stribiżew
          Sep 27 '18 at 15:28



















        • That pertains to both regular string literals as well as template string literals.

          – Wiktor Stribiżew
          Sep 27 '18 at 15:28

















        That pertains to both regular string literals as well as template string literals.

        – Wiktor Stribiżew
        Sep 27 '18 at 15:28





        That pertains to both regular string literals as well as template string literals.

        – Wiktor Stribiżew
        Sep 27 '18 at 15:28













        13














        Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t, n, ", etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.



        So when you generate a string by saying var someString = '(\s|^)', what you're really doing is creating an actual string with the value (s|^).






        share|improve this answer




























          13














          Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t, n, ", etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.



          So when you generate a string by saying var someString = '(\s|^)', what you're really doing is creating an actual string with the value (s|^).






          share|improve this answer


























            13












            13








            13







            Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t, n, ", etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.



            So when you generate a string by saying var someString = '(\s|^)', what you're really doing is creating an actual string with the value (s|^).






            share|improve this answer













            Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t, n, ", etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.



            So when you generate a string by saying var someString = '(\s|^)', what you're really doing is creating an actual string with the value (s|^).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 25 '13 at 16:02









            Joe EnosJoe Enos

            30.8k1060122




            30.8k1060122























                4














                is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .



                EDIT: Even had to do it here, because \ in my answer turned to .






                share|improve this answer






























                  4














                  is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .



                  EDIT: Even had to do it here, because \ in my answer turned to .






                  share|improve this answer




























                    4












                    4








                    4







                    is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .



                    EDIT: Even had to do it here, because \ in my answer turned to .






                    share|improve this answer















                    is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .



                    EDIT: Even had to do it here, because \ in my answer turned to .







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 27 '13 at 16:58

























                    answered Jul 25 '13 at 16:04









                    schlichtschlicht

                    3,5671920




                    3,5671920























                        4














                        The Regex needs a string representation of s, which in JavaScript can be produced using the literal "\s".



                        Here's a live example to illustrate why "s" is not enough:






                        alert("One backslash:          snDouble backslashes: \s");





                        Note how an extra before s changes the output.






                        share|improve this answer






























                          4














                          The Regex needs a string representation of s, which in JavaScript can be produced using the literal "\s".



                          Here's a live example to illustrate why "s" is not enough:






                          alert("One backslash:          snDouble backslashes: \s");





                          Note how an extra before s changes the output.






                          share|improve this answer




























                            4












                            4








                            4







                            The Regex needs a string representation of s, which in JavaScript can be produced using the literal "\s".



                            Here's a live example to illustrate why "s" is not enough:






                            alert("One backslash:          snDouble backslashes: \s");





                            Note how an extra before s changes the output.






                            share|improve this answer















                            The Regex needs a string representation of s, which in JavaScript can be produced using the literal "\s".



                            Here's a live example to illustrate why "s" is not enough:






                            alert("One backslash:          snDouble backslashes: \s");





                            Note how an extra before s changes the output.






                            alert("One backslash:          snDouble backslashes: \s");





                            alert("One backslash:          snDouble backslashes: \s");






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 22 '16 at 7:40









                            Wiktor Stribiżew

                            322k16142223




                            322k16142223










                            answered Jul 25 '13 at 16:02









                            Cristian LupascuCristian Lupascu

                            27.9k1078116




                            27.9k1078116






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17863066%2fwhy-do-regex-constructors-need-to-be-double-escaped%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                Npm cannot find a required file even through it is in the searched directory