0

I was looking at the responses to the question at Removing backslashes from strings in javascript and found that both the responses work,

string.replace(/\\\//g, "/");

and

str = str.replace(/\\/g, '');

Could someone please explain the difference between these two and which would be a better choice?

1
  • What exactly is your requirement for this replacement? Commented Feb 9, 2014 at 14:01

2 Answers 2

1

The first one unescapes forward slashes specifically (ie replacing \/ with /)

The second just removes all backslashes.

Sign up to request clarification or add additional context in comments.

1 Comment

is that necessary (replacing the forward slashes I mean)?
0

The second solution str = str.replace(/\\/g, ''); is wrong, because it will remove single \ from the string.

2 Comments

Actually it removes all backslashes from the string. Could you please give the example you tried where it did not?
The question was to remove \\ (consecutive backslashes) not \ (single backslash).

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.