Replace does not work when replacing text inside a span element










1















I have a script that puts math questions (from a mySQL database) on a page. For instance



  • Find 10% of 50

  • Find 10% of 60

  • Find 10% of 55

You get the idea.



I've written a script to avoid repeating the first word. It's overly long and complicated but I wrote it and I'm new to this and pretty proud that i did it.



Anyway, this script takes the first word and puts it in a variable called introtext.



I then am trying to take my questions and remove this introtext so it's not repeated



var q = q.replace(p1introtext,"");


This works really well when the value of introtext is something like 'simplify' 'find' or even a reasonably long sentence. However if the intro text contains html formatting like



<span class = "smaller">Find</span>


then the " are causing the program to ignore the replace function.



I do realize there is an element of bad database design here, but it's a big one and it will take ages to get rid of all the span tags, which are actually quite useful when using the questions in other formats.



I cannot just hide the .smaller class using css as it can also appear in bits of the question I want to display.



I'm really confused as to how I can work with this










share|improve this question



















  • 1





    q = <span="someClass">aVariable</span> isn't valid Javascript, are there meant to be quotes around it? i.e. q = '<span="someClass">aVariable</span>'

    – Jayce444
    Nov 16 '18 at 5:25











  • also .replace(aVariable, ""); will replace whatever the value of the variable aVariable is with an empty string .. it doesn't replace the string "aVariable" ... e.g. if aVariable = "fred" then .replace(aVariable, ""); is equivalent of .replace("fred", "");

    – Bravo
    Nov 16 '18 at 5:28












  • Yeah! That's what I want.

    – Richard Tock
    Nov 16 '18 at 5:31















1















I have a script that puts math questions (from a mySQL database) on a page. For instance



  • Find 10% of 50

  • Find 10% of 60

  • Find 10% of 55

You get the idea.



I've written a script to avoid repeating the first word. It's overly long and complicated but I wrote it and I'm new to this and pretty proud that i did it.



Anyway, this script takes the first word and puts it in a variable called introtext.



I then am trying to take my questions and remove this introtext so it's not repeated



var q = q.replace(p1introtext,"");


This works really well when the value of introtext is something like 'simplify' 'find' or even a reasonably long sentence. However if the intro text contains html formatting like



<span class = "smaller">Find</span>


then the " are causing the program to ignore the replace function.



I do realize there is an element of bad database design here, but it's a big one and it will take ages to get rid of all the span tags, which are actually quite useful when using the questions in other formats.



I cannot just hide the .smaller class using css as it can also appear in bits of the question I want to display.



I'm really confused as to how I can work with this










share|improve this question



















  • 1





    q = <span="someClass">aVariable</span> isn't valid Javascript, are there meant to be quotes around it? i.e. q = '<span="someClass">aVariable</span>'

    – Jayce444
    Nov 16 '18 at 5:25











  • also .replace(aVariable, ""); will replace whatever the value of the variable aVariable is with an empty string .. it doesn't replace the string "aVariable" ... e.g. if aVariable = "fred" then .replace(aVariable, ""); is equivalent of .replace("fred", "");

    – Bravo
    Nov 16 '18 at 5:28












  • Yeah! That's what I want.

    – Richard Tock
    Nov 16 '18 at 5:31













1












1








1








I have a script that puts math questions (from a mySQL database) on a page. For instance



  • Find 10% of 50

  • Find 10% of 60

  • Find 10% of 55

You get the idea.



I've written a script to avoid repeating the first word. It's overly long and complicated but I wrote it and I'm new to this and pretty proud that i did it.



Anyway, this script takes the first word and puts it in a variable called introtext.



I then am trying to take my questions and remove this introtext so it's not repeated



var q = q.replace(p1introtext,"");


This works really well when the value of introtext is something like 'simplify' 'find' or even a reasonably long sentence. However if the intro text contains html formatting like



<span class = "smaller">Find</span>


then the " are causing the program to ignore the replace function.



I do realize there is an element of bad database design here, but it's a big one and it will take ages to get rid of all the span tags, which are actually quite useful when using the questions in other formats.



I cannot just hide the .smaller class using css as it can also appear in bits of the question I want to display.



I'm really confused as to how I can work with this










share|improve this question
















I have a script that puts math questions (from a mySQL database) on a page. For instance



  • Find 10% of 50

  • Find 10% of 60

  • Find 10% of 55

You get the idea.



I've written a script to avoid repeating the first word. It's overly long and complicated but I wrote it and I'm new to this and pretty proud that i did it.



Anyway, this script takes the first word and puts it in a variable called introtext.



I then am trying to take my questions and remove this introtext so it's not repeated



var q = q.replace(p1introtext,"");


This works really well when the value of introtext is something like 'simplify' 'find' or even a reasonably long sentence. However if the intro text contains html formatting like



<span class = "smaller">Find</span>


then the " are causing the program to ignore the replace function.



I do realize there is an element of bad database design here, but it's a big one and it will take ages to get rid of all the span tags, which are actually quite useful when using the questions in other formats.



I cannot just hide the .smaller class using css as it can also appear in bits of the question I want to display.



I'm really confused as to how I can work with this







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:51







Richard Tock

















asked Nov 16 '18 at 5:22









Richard TockRichard Tock

278




278







  • 1





    q = <span="someClass">aVariable</span> isn't valid Javascript, are there meant to be quotes around it? i.e. q = '<span="someClass">aVariable</span>'

    – Jayce444
    Nov 16 '18 at 5:25











  • also .replace(aVariable, ""); will replace whatever the value of the variable aVariable is with an empty string .. it doesn't replace the string "aVariable" ... e.g. if aVariable = "fred" then .replace(aVariable, ""); is equivalent of .replace("fred", "");

    – Bravo
    Nov 16 '18 at 5:28












  • Yeah! That's what I want.

    – Richard Tock
    Nov 16 '18 at 5:31












  • 1





    q = <span="someClass">aVariable</span> isn't valid Javascript, are there meant to be quotes around it? i.e. q = '<span="someClass">aVariable</span>'

    – Jayce444
    Nov 16 '18 at 5:25











  • also .replace(aVariable, ""); will replace whatever the value of the variable aVariable is with an empty string .. it doesn't replace the string "aVariable" ... e.g. if aVariable = "fred" then .replace(aVariable, ""); is equivalent of .replace("fred", "");

    – Bravo
    Nov 16 '18 at 5:28












  • Yeah! That's what I want.

    – Richard Tock
    Nov 16 '18 at 5:31







1




1





q = <span="someClass">aVariable</span> isn't valid Javascript, are there meant to be quotes around it? i.e. q = '<span="someClass">aVariable</span>'

– Jayce444
Nov 16 '18 at 5:25





q = <span="someClass">aVariable</span> isn't valid Javascript, are there meant to be quotes around it? i.e. q = '<span="someClass">aVariable</span>'

– Jayce444
Nov 16 '18 at 5:25













also .replace(aVariable, ""); will replace whatever the value of the variable aVariable is with an empty string .. it doesn't replace the string "aVariable" ... e.g. if aVariable = "fred" then .replace(aVariable, ""); is equivalent of .replace("fred", "");

– Bravo
Nov 16 '18 at 5:28






also .replace(aVariable, ""); will replace whatever the value of the variable aVariable is with an empty string .. it doesn't replace the string "aVariable" ... e.g. if aVariable = "fred" then .replace(aVariable, ""); is equivalent of .replace("fred", "");

– Bravo
Nov 16 '18 at 5:28














Yeah! That's what I want.

– Richard Tock
Nov 16 '18 at 5:31





Yeah! That's what I want.

– Richard Tock
Nov 16 '18 at 5:31












4 Answers
4






active

oldest

votes


















0














You have to enclose your text in single quotes since you're using double quotes in your text.



q = '<span="someClass">aVariable</span>';





share|improve this answer






























    0

















    var q = '<span="someClass">aVariable</span>';
    q = q.replace("aVariable", "");
    console.log(q);

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





    try this one



    var q = '<span="someClass">aVariable</span>';
    q = q.replace("aVariable", "");
    console.log(q);





    share|improve this answer






























      0

















      var q = '<span="someClass">aVariable</span>';
      q = q.replace('aVariable', "");
      console.log(q);








      share|improve this answer






























        0














        Use backticks with template literals and aVariable is not a variable in your code, it's a string



        var q = `<span="someClass">aVariable</span>`;
        q = q.replace("aVariable", "a");


        Working code






        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%2f53331886%2freplace-does-not-work-when-replacing-text-inside-a-span-element%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









          0














          You have to enclose your text in single quotes since you're using double quotes in your text.



          q = '<span="someClass">aVariable</span>';





          share|improve this answer



























            0














            You have to enclose your text in single quotes since you're using double quotes in your text.



            q = '<span="someClass">aVariable</span>';





            share|improve this answer

























              0












              0








              0







              You have to enclose your text in single quotes since you're using double quotes in your text.



              q = '<span="someClass">aVariable</span>';





              share|improve this answer













              You have to enclose your text in single quotes since you're using double quotes in your text.



              q = '<span="someClass">aVariable</span>';






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 16 '18 at 5:26









              DifsterDifster

              3,02811730




              3,02811730























                  0

















                  var q = '<span="someClass">aVariable</span>';
                  q = q.replace("aVariable", "");
                  console.log(q);

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





                  try this one



                  var q = '<span="someClass">aVariable</span>';
                  q = q.replace("aVariable", "");
                  console.log(q);





                  share|improve this answer



























                    0

















                    var q = '<span="someClass">aVariable</span>';
                    q = q.replace("aVariable", "");
                    console.log(q);

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





                    try this one



                    var q = '<span="someClass">aVariable</span>';
                    q = q.replace("aVariable", "");
                    console.log(q);





                    share|improve this answer

























                      0












                      0








                      0










                      var q = '<span="someClass">aVariable</span>';
                      q = q.replace("aVariable", "");
                      console.log(q);

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





                      try this one



                      var q = '<span="someClass">aVariable</span>';
                      q = q.replace("aVariable", "");
                      console.log(q);





                      share|improve this answer
















                      var q = '<span="someClass">aVariable</span>';
                      q = q.replace("aVariable", "");
                      console.log(q);

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





                      try this one



                      var q = '<span="someClass">aVariable</span>';
                      q = q.replace("aVariable", "");
                      console.log(q);





                      var q = '<span="someClass">aVariable</span>';
                      q = q.replace("aVariable", "");
                      console.log(q);

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





                      var q = '<span="someClass">aVariable</span>';
                      q = q.replace("aVariable", "");
                      console.log(q);

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 5:29









                      Manish VadherManish Vadher

                      509412




                      509412





















                          0

















                          var q = '<span="someClass">aVariable</span>';
                          q = q.replace('aVariable', "");
                          console.log(q);








                          share|improve this answer



























                            0

















                            var q = '<span="someClass">aVariable</span>';
                            q = q.replace('aVariable', "");
                            console.log(q);








                            share|improve this answer

























                              0












                              0








                              0










                              var q = '<span="someClass">aVariable</span>';
                              q = q.replace('aVariable', "");
                              console.log(q);








                              share|improve this answer
















                              var q = '<span="someClass">aVariable</span>';
                              q = q.replace('aVariable', "");
                              console.log(q);








                              var q = '<span="someClass">aVariable</span>';
                              q = q.replace('aVariable', "");
                              console.log(q);





                              var q = '<span="someClass">aVariable</span>';
                              q = q.replace('aVariable', "");
                              console.log(q);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 16 '18 at 5:30









                              ACDACD

                              8941213




                              8941213





















                                  0














                                  Use backticks with template literals and aVariable is not a variable in your code, it's a string



                                  var q = `<span="someClass">aVariable</span>`;
                                  q = q.replace("aVariable", "a");


                                  Working code






                                  share|improve this answer



























                                    0














                                    Use backticks with template literals and aVariable is not a variable in your code, it's a string



                                    var q = `<span="someClass">aVariable</span>`;
                                    q = q.replace("aVariable", "a");


                                    Working code






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      Use backticks with template literals and aVariable is not a variable in your code, it's a string



                                      var q = `<span="someClass">aVariable</span>`;
                                      q = q.replace("aVariable", "a");


                                      Working code






                                      share|improve this answer













                                      Use backticks with template literals and aVariable is not a variable in your code, it's a string



                                      var q = `<span="someClass">aVariable</span>`;
                                      q = q.replace("aVariable", "a");


                                      Working code







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 16 '18 at 5:33









                                      Prithivi RajPrithivi Raj

                                      1,83511026




                                      1,83511026



























                                          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%2f53331886%2freplace-does-not-work-when-replacing-text-inside-a-span-element%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

                                          Top Tejano songwriter Luis Silva dead of heart attack at 64

                                          ReactJS Fetched API data displays live - need Data displayed static

                                          政党