How to use String.raw on array variables





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I have a problem in my code, i have defined arrays but it contain special characters like /, {}, (), , etc... (because of plainsource MathJax)



The array returns the string without those mentioned special characters



PS: I've read about String.raw but i have no idea how to use it in this kind of array. Help me please :(



var Array_QA = {
"Large {lim_{x to 2}} (5^x + 2^x + 4)" : "33",
"Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
};









share|improve this question































    3















    I have a problem in my code, i have defined arrays but it contain special characters like /, {}, (), , etc... (because of plainsource MathJax)



    The array returns the string without those mentioned special characters



    PS: I've read about String.raw but i have no idea how to use it in this kind of array. Help me please :(



    var Array_QA = {
    "Large {lim_{x to 2}} (5^x + 2^x + 4)" : "33",
    "Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
    };









    share|improve this question



























      3












      3








      3








      I have a problem in my code, i have defined arrays but it contain special characters like /, {}, (), , etc... (because of plainsource MathJax)



      The array returns the string without those mentioned special characters



      PS: I've read about String.raw but i have no idea how to use it in this kind of array. Help me please :(



      var Array_QA = {
      "Large {lim_{x to 2}} (5^x + 2^x + 4)" : "33",
      "Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
      };









      share|improve this question
















      I have a problem in my code, i have defined arrays but it contain special characters like /, {}, (), , etc... (because of plainsource MathJax)



      The array returns the string without those mentioned special characters



      PS: I've read about String.raw but i have no idea how to use it in this kind of array. Help me please :(



      var Array_QA = {
      "Large {lim_{x to 2}} (5^x + 2^x + 4)" : "33",
      "Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
      };






      javascript jquery mathjax






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 12:09









      Jai

      64.5k105782




      64.5k105782










      asked Jan 4 at 12:06









      Acz FlorencioAcz Florencio

      161




      161
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Because the backslash is an escape character in javascript strings, you need to double it if you want it to appear in a string literal. Try



          var Array_QA = {
          "\Large {\lim_{x \to 2}} (5^x + 2^x + 4)" : "33",
          "\Large {\lim_{x \to 2}} \frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
          };





          share|improve this answer































            0














            String.raw is used with template literals, the new(ish) ES6 syntax for strings with interpolated variables, embedded quotes and newlines, and the optional application of "tags" (which are just function callbacks that receive the pieces of the template split up by each interpolated variable to return the final combined string however you like).



            So if I have a function that processes a template literal, and it's called processLiteral, then I could do something like this to apply it to a literal:



            processLiteral`This is a string with interpolated variables like ${someVar} and also ${otherVar}.`


            Specifically, String.raw is meant to be used as the tag of a template literal, and it will return the raw string without processing any escaped characters. Meaning n will remain as n and not become a newline, t will remain as t and not become a tab, etc. It does this by automatically adding slashes before any escaping slashes, thereby escaping them, so it's equivalent to using \n, \t, etc.



            So in your case, if you want to use that method, since you can't put template literals directly in an object key declaration (it doesn't parse correctly), you'd do something like this:



            var Array_QA = {};
            Array_QA[String.raw`Large {lim_{x to 2}} (5^x + 2^x + 4)`] = "33";
            Array_QA[String.raw`Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}`] = "0";


            That said, having complex strings like that as object keys is probably not a good idea from the start... but it should work. I'd suggest refactoring your code to do that differently, if you can.






            share|improve this answer



















            • 1





              You could use them in computed property keys.

              – Bergi
              Jan 7 at 20:47












            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%2f54038669%2fhow-to-use-string-raw-on-array-variables%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Because the backslash is an escape character in javascript strings, you need to double it if you want it to appear in a string literal. Try



            var Array_QA = {
            "\Large {\lim_{x \to 2}} (5^x + 2^x + 4)" : "33",
            "\Large {\lim_{x \to 2}} \frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
            };





            share|improve this answer




























              0














              Because the backslash is an escape character in javascript strings, you need to double it if you want it to appear in a string literal. Try



              var Array_QA = {
              "\Large {\lim_{x \to 2}} (5^x + 2^x + 4)" : "33",
              "\Large {\lim_{x \to 2}} \frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
              };





              share|improve this answer


























                0












                0








                0







                Because the backslash is an escape character in javascript strings, you need to double it if you want it to appear in a string literal. Try



                var Array_QA = {
                "\Large {\lim_{x \to 2}} (5^x + 2^x + 4)" : "33",
                "\Large {\lim_{x \to 2}} \frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
                };





                share|improve this answer













                Because the backslash is an escape character in javascript strings, you need to double it if you want it to appear in a string literal. Try



                var Array_QA = {
                "\Large {\lim_{x \to 2}} (5^x + 2^x + 4)" : "33",
                "\Large {\lim_{x \to 2}} \frac {x^2 - 6x + 8}{x^3 - 4}" : "0"
                };






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 5 at 18:14









                Davide CervoneDavide Cervone

                7,3531936




                7,3531936

























                    0














                    String.raw is used with template literals, the new(ish) ES6 syntax for strings with interpolated variables, embedded quotes and newlines, and the optional application of "tags" (which are just function callbacks that receive the pieces of the template split up by each interpolated variable to return the final combined string however you like).



                    So if I have a function that processes a template literal, and it's called processLiteral, then I could do something like this to apply it to a literal:



                    processLiteral`This is a string with interpolated variables like ${someVar} and also ${otherVar}.`


                    Specifically, String.raw is meant to be used as the tag of a template literal, and it will return the raw string without processing any escaped characters. Meaning n will remain as n and not become a newline, t will remain as t and not become a tab, etc. It does this by automatically adding slashes before any escaping slashes, thereby escaping them, so it's equivalent to using \n, \t, etc.



                    So in your case, if you want to use that method, since you can't put template literals directly in an object key declaration (it doesn't parse correctly), you'd do something like this:



                    var Array_QA = {};
                    Array_QA[String.raw`Large {lim_{x to 2}} (5^x + 2^x + 4)`] = "33";
                    Array_QA[String.raw`Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}`] = "0";


                    That said, having complex strings like that as object keys is probably not a good idea from the start... but it should work. I'd suggest refactoring your code to do that differently, if you can.






                    share|improve this answer



















                    • 1





                      You could use them in computed property keys.

                      – Bergi
                      Jan 7 at 20:47
















                    0














                    String.raw is used with template literals, the new(ish) ES6 syntax for strings with interpolated variables, embedded quotes and newlines, and the optional application of "tags" (which are just function callbacks that receive the pieces of the template split up by each interpolated variable to return the final combined string however you like).



                    So if I have a function that processes a template literal, and it's called processLiteral, then I could do something like this to apply it to a literal:



                    processLiteral`This is a string with interpolated variables like ${someVar} and also ${otherVar}.`


                    Specifically, String.raw is meant to be used as the tag of a template literal, and it will return the raw string without processing any escaped characters. Meaning n will remain as n and not become a newline, t will remain as t and not become a tab, etc. It does this by automatically adding slashes before any escaping slashes, thereby escaping them, so it's equivalent to using \n, \t, etc.



                    So in your case, if you want to use that method, since you can't put template literals directly in an object key declaration (it doesn't parse correctly), you'd do something like this:



                    var Array_QA = {};
                    Array_QA[String.raw`Large {lim_{x to 2}} (5^x + 2^x + 4)`] = "33";
                    Array_QA[String.raw`Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}`] = "0";


                    That said, having complex strings like that as object keys is probably not a good idea from the start... but it should work. I'd suggest refactoring your code to do that differently, if you can.






                    share|improve this answer



















                    • 1





                      You could use them in computed property keys.

                      – Bergi
                      Jan 7 at 20:47














                    0












                    0








                    0







                    String.raw is used with template literals, the new(ish) ES6 syntax for strings with interpolated variables, embedded quotes and newlines, and the optional application of "tags" (which are just function callbacks that receive the pieces of the template split up by each interpolated variable to return the final combined string however you like).



                    So if I have a function that processes a template literal, and it's called processLiteral, then I could do something like this to apply it to a literal:



                    processLiteral`This is a string with interpolated variables like ${someVar} and also ${otherVar}.`


                    Specifically, String.raw is meant to be used as the tag of a template literal, and it will return the raw string without processing any escaped characters. Meaning n will remain as n and not become a newline, t will remain as t and not become a tab, etc. It does this by automatically adding slashes before any escaping slashes, thereby escaping them, so it's equivalent to using \n, \t, etc.



                    So in your case, if you want to use that method, since you can't put template literals directly in an object key declaration (it doesn't parse correctly), you'd do something like this:



                    var Array_QA = {};
                    Array_QA[String.raw`Large {lim_{x to 2}} (5^x + 2^x + 4)`] = "33";
                    Array_QA[String.raw`Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}`] = "0";


                    That said, having complex strings like that as object keys is probably not a good idea from the start... but it should work. I'd suggest refactoring your code to do that differently, if you can.






                    share|improve this answer













                    String.raw is used with template literals, the new(ish) ES6 syntax for strings with interpolated variables, embedded quotes and newlines, and the optional application of "tags" (which are just function callbacks that receive the pieces of the template split up by each interpolated variable to return the final combined string however you like).



                    So if I have a function that processes a template literal, and it's called processLiteral, then I could do something like this to apply it to a literal:



                    processLiteral`This is a string with interpolated variables like ${someVar} and also ${otherVar}.`


                    Specifically, String.raw is meant to be used as the tag of a template literal, and it will return the raw string without processing any escaped characters. Meaning n will remain as n and not become a newline, t will remain as t and not become a tab, etc. It does this by automatically adding slashes before any escaping slashes, thereby escaping them, so it's equivalent to using \n, \t, etc.



                    So in your case, if you want to use that method, since you can't put template literals directly in an object key declaration (it doesn't parse correctly), you'd do something like this:



                    var Array_QA = {};
                    Array_QA[String.raw`Large {lim_{x to 2}} (5^x + 2^x + 4)`] = "33";
                    Array_QA[String.raw`Large {lim_{x to 2}} frac {x^2 - 6x + 8}{x^3 - 4}`] = "0";


                    That said, having complex strings like that as object keys is probably not a good idea from the start... but it should work. I'd suggest refactoring your code to do that differently, if you can.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 7 at 20:20









                    IceMetalPunkIceMetalPunk

                    1,034816




                    1,034816








                    • 1





                      You could use them in computed property keys.

                      – Bergi
                      Jan 7 at 20:47














                    • 1





                      You could use them in computed property keys.

                      – Bergi
                      Jan 7 at 20:47








                    1




                    1





                    You could use them in computed property keys.

                    – Bergi
                    Jan 7 at 20:47





                    You could use them in computed property keys.

                    – Bergi
                    Jan 7 at 20:47


















                    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%2f54038669%2fhow-to-use-string-raw-on-array-variables%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

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas