0

i need help in this:i want to remove from a string a specific value. for example i have this code:

document.getElementById("sub_subject_temp"+id1).value = old_sub_subject_string.replace(/theVal/,"");

if my string is:

6-7,6-3,6-1,

and i want to remove:

6-3,

it would look like this: 6-7,6-1, i have tryed the

str.replace(/val/, "");

but it return 6, any idea to do this thing?

4 Answers 4

2
"6-7,6-3,6-1".replace(/6-3,/, '');

//"6-7,6-1"
Sign up to request clarification or add additional context in comments.

Comments

0

How about replacing the string you want to remove, not "val"? Try

str.replace(/6-3,/, "");

Comments

0

If you mean you use a variable val whose value is like 6-3;, you need to use new RegExp() to construct the regex.

var str = '6-7,6-3,6-1,';
var val = '6-3,';
str = str.replace(new RegExp(val), '');

Comments

0

There is a way to do this without .replace. Although it is more complex, it works.

var rstring = "6-7,6-3,6-1";
var nstring = rstring.split("6-3,");
var finishedstr = nstring[0] + nstring[1];

So, in the above code the original string is split into two while removing "6-3," from the string. The two new sub-strings are recombined without the "6-3,", allowing a finished string of "6-7,6-1", or in this case, the variable finishedstr.

1 Comment

Hope this helps! If anybody has questions, feel free to comment them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.