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\xy\ab\1324\1324.jpg";
var final = test.replace(/\\/g,"/");
Output
http:/www.xyzxyab13241324.jpg
Please Let me know where I'm going wrong , Thank you
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.\xy\ab\1324\1324.jpg";
var final = test.replace(/\\/g,"/");
Output
http:/www.xyz.xyab13241324.jpg
Please Let me know where I'm going wrong , Thank you
Share Improve this question edited Jun 18, 2016 at 13:41 Zac 84110 silver badges26 bronze badges asked Jun 18, 2016 at 11:11 Akhil RJAkhil RJ 3571 gold badge4 silver badges20 bronze badges 6- you should have double slash after http --- xyz.xyab13241324.jpg – Parag Bhayani Commented Jun 18, 2016 at 11:16
-
var test = "http:\\www..."
means that the value ofvar
ishttp:\www...
. What is the actual string which is ing in? If it actually contains true backslash characters, then your regexp should work fine. – user663031 Commented Jun 18, 2016 at 11:17 -
var test ="http:\\www.xyz.\xy\ab\1324\1324.jpg";
throws error, because it is incorrectly escaped. It is not a valid string. – Billy Moon Commented Jun 18, 2016 at 11:17 - Why do you have URIs with backslashes? stackoverflow./questions/3903488/… – Sebastian G. Marinescu Commented Jun 18, 2016 at 11:20
- Sorry but the provided test data is ing from the API only so , is their any work around , with the data i have :( – Akhil RJ Commented Jun 18, 2016 at 11:29
3 Answers
Reset to default 1This 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.\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.yyabZ4Z4.jpg"
To visualize it even more, enter into your console: 'http:\\www.xyz.\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.
Its working fine with escaped backslashes.
var test ="http:\\\\www.xyz.\\xy\\ab\\1324\\1324.jpg";
var final = test.replace(/\\/g,"/");
console.log(final);
Taking a guess at piecing all of these things together:
- Sebastian is correct in that something like
var test ="http:\\www.xyz.\xy\ab\1324\1324.jpg";
is not valid HTML. - However, no API in javascript will actually easily allow you to create such a string. So, the real question is where is the string ing 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.
- 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 wantvar test = http:\\\\www.xyz.
One way to check your work is to use JSON.stringify
to e up with the actual value that you need to type in your source code. E.g.:
var test = 'http:\\\\xyz.';
console.log(test); // prints "http:\\xyz."
console.log(JSON.stringify(test)); // prints "http:\\\\xyz."