4

I'm trying to replace backslashes with forward slashes in javascript with the replace function and regex but can't get it to work. Thought it would be the following but it doesn't work:

path.replace("/\\/g", "//")

If I had a path that looks like D:\Games\Scrolls\ what would the regex be to replace to D:/Games/Scrolls/?

1
  • Remove the quotes from around your regex as shown in the answers in the duplicate linked to your previous question. I.e., it should be path.replace(/\\/g, "/") - note also the replacement string should be "/" not "//". Commented Apr 20, 2017 at 2:15

2 Answers 2

10

Use the regex pattern without a string type
path.replace(/\\/g, "/")

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

1 Comment

Dangit was caught up in the normal text way. Thank you.
2

This should do the trick

path=path.replace(/\\/g,"/");

Comments

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.