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






  • 1




    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 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






  • 1




    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 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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 25 '13 at 15:58









Smurfette

5351615




5351615












  • 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






  • 1




    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 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






  • 1




    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 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




1




1




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 at 9:00




Related: stackoverflow.com/a/37329801/1225328.
– sp00m
Jul 30 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 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














    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































      3














      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























        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 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 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









        Quentin

        638k718611031




        638k718611031












        • That pertains to both regular string literals as well as template string literals.
          – Wiktor Stribiżew
          Sep 27 at 15:28


















        • That pertains to both regular string literals as well as template string literals.
          – Wiktor Stribiżew
          Sep 27 at 15:28
















        That pertains to both regular string literals as well as template string literals.
        – Wiktor Stribiżew
        Sep 27 at 15:28




        That pertains to both regular string literals as well as template string literals.
        – Wiktor Stribiżew
        Sep 27 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 Enos

            30.5k1059122




            30.5k1059122























                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

                    307k16126202




                    307k16126202










                    answered Jul 25 '13 at 16:02









                    Cristian Lupascu

                    27.3k1075115




                    27.3k1075115























                        3














                        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




























                          3














                          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


























                            3












                            3








                            3






                            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









                            schlicht

                            3,4271920




                            3,4271920






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                Refactoring coordinates for Minecraft Pi buildings written in Python