Replace does not work when replacing text inside a span element

Multi tool use
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
add a comment |
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
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 variableaVariable
is with an empty string .. it doesn't replace the string"aVariable"
... e.g. ifaVariable = "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
add a comment |
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
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
javascript
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 variableaVariable
is with an empty string .. it doesn't replace the string"aVariable"
... e.g. ifaVariable = "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
add a comment |
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 variableaVariable
is with an empty string .. it doesn't replace the string"aVariable"
... e.g. ifaVariable = "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
add a comment |
4 Answers
4
active
oldest
votes
You have to enclose your text in single quotes since you're using double quotes in your text.
q = '<span="someClass">aVariable</span>';
add a comment |
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);
add a comment |
var q = '<span="someClass">aVariable</span>';
q = q.replace('aVariable', "");
console.log(q);
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You have to enclose your text in single quotes since you're using double quotes in your text.
q = '<span="someClass">aVariable</span>';
add a comment |
You have to enclose your text in single quotes since you're using double quotes in your text.
q = '<span="someClass">aVariable</span>';
add a comment |
You have to enclose your text in single quotes since you're using double quotes in your text.
q = '<span="someClass">aVariable</span>';
You have to enclose your text in single quotes since you're using double quotes in your text.
q = '<span="someClass">aVariable</span>';
answered Nov 16 '18 at 5:26


DifsterDifster
3,02811730
3,02811730
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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>
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>
answered Nov 16 '18 at 5:29
Manish VadherManish Vadher
509412
509412
add a comment |
add a comment |
var q = '<span="someClass">aVariable</span>';
q = q.replace('aVariable', "");
console.log(q);
add a comment |
var q = '<span="someClass">aVariable</span>';
q = q.replace('aVariable', "");
console.log(q);
add a comment |
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);
var q = '<span="someClass">aVariable</span>';
q = q.replace('aVariable', "");
console.log(q);
answered Nov 16 '18 at 5:30


ACDACD
8941213
8941213
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 16 '18 at 5:33


Prithivi RajPrithivi Raj
1,83511026
1,83511026
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
038bXS5Q3Y8Msoe5zpP4lvQwj uV0 8SbIo
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 variableaVariable
is with an empty string .. it doesn't replace the string"aVariable"
... e.g. ifaVariable = "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