Akhil RJ
Akhil RJ

Reputation: 357

Replace Backward Slashes with Forward Slashes Javascript

I'm Getting some images from API with Backward Slashes , and when I tried to display these images on my page its working fine on Chrome but on other browsers like FireFox and IE it's not working , after some googling I get to know that I have to pass URL with forward slashes , So I tried replacing it but it's not working ..

Following is the code that I tried...

Input

var test ="http:\\www.xyz.com\xy\ab\1324\1324.jpg";
var final = test.replace(/\\/g,"/");

Output

http:/www.xyz.comxyab13241324.jpg

Please Let me know where I'm going wrong , Thank you

Upvotes: 1

Views: 13770

Answers (3)

Sebastian G. Marinescu
Sebastian G. Marinescu

Reputation: 2394

This is not possible — with the provided example-string or anything similar.

\x is the first problem here. JavaScript thinks this is a Hexadecimal escape sequence, that's why the JavaScript-Interpreter is throwing an appropriate error:

Uncaught SyntaxError: Invalid hexadecimal escape sequence

And even if we take another example string: 'http:\\www.xyz.com\yy\ab\1324\1324.jpg' it will fail.
JavaScript thinks that the backslashes are there to escape something as Octal escape sequence — that is why just entering this string into a JS-Console and hitting return gives you back:

"http:\www.xyz.comyyabZ4Z4.jpg"

To visualize it even more, enter into your console: 'http:\\www.xyz.com\yy\ab\1324\1324.jpg'.split('');

You'll see that even \132 gets converted to Z.

I tried many things right now, like replacing/escaping, trying JSON.stringify, using a text-node, using CDATA inside a virtual XML-document, etc. etc. – nothing worked. If somebody finds a JavaScript-way for doing this, I'd be happy to know about it!


Conclusion

I don't know of any way for doing this inside JavaScript. There seems to be no chance.

Your only solution as I see it, is to escape it on the server-side.
In your case you will have to write a little server-script, that calls your used API and converts/escapes everything to be ready for your JS. And your JS calls this little server-script.

Upvotes: 1

intentionally-left-nil
intentionally-left-nil

Reputation: 8284

Taking a guess at piecing all of these things together:

  1. Sebastian is correct in that something like var test ="http:\\www.xyz.com\xy\ab\1324\1324.jpg"; is not valid HTML.
  2. However, no API in javascript will actually easily allow you to create such a string. So, the real question is where is the string coming from? If it is some kind of buffer being decoded then your problem lies in that library, wherever it is. Given that that has to be javascript somewhere, your best bet would be to either modify the source or monkey patch it at runtime.
  3. My spidey sense tells me that your variable is a proper string, and that your regex would work properly if inserted into your real code. However, for your test case, to create a string with the \ character, you have to escape it, so you would want var test = http:\\\\www.xyz.com

One way to check your work is to use JSON.stringify to come up with the actual value that you need to type in your source code. E.g.:

var test = 'http:\\\\xyz.com';
console.log(test); // prints "http:\\xyz.com"
console.log(JSON.stringify(test)); // prints "http:\\\\xyz.com"

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386578

Its working fine with escaped backslashes.

var test ="http:\\\\www.xyz.com\\xy\\ab\\1324\\1324.jpg";
var final = test.replace(/\\/g,"/");

console.log(final);

Upvotes: 0

Related Questions