0

I have url that i need to change to a valid slashes.

var url = data\14/loto/posts.json

I need this to change to this:

data/14/loto/posts.json

But this is not working:

url.replace('\', '/');
2
  • Escape the slash using a double slash. Commented Dec 6, 2014 at 18:07
  • 2
    If you didn't see the SyntaxError: unterminated string literal error message, please find your browser's JavaScript console. It's a basic tool to debug JavaScript. Commented Dec 6, 2014 at 18:09

2 Answers 2

1

In JS, you need to escape backslashes, because they are normally escape characters themselves.

url.replace('\\', '/');

Additionally, if you want to escape multiple backslashes in the same string, use a regex literal with the g flag, "g" standing for "global".

url.replace(/\\/g, '/');

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

Comments

1

Should be

var url = "data\\14/loto/posts.json" // "\\" is because slash should be escaped, otherwise your url isn't a valid string
url = url.replace(/\\/g, '/');

Comments