-3

I have the following array:

[ 'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'67c8ca21-bcdf-4298-
aaf0-ae23c18510f1\')/$value',
'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'313fbef3-f53c-456f-
be75-1002a3b3e2a5\')/$value',
'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'31df567e-399b-4065-
9399-75f4233cba5f\')/$value',
'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'f643e5f4-3791-436e-
 b304-4d451391af4d\')/$value',
'https://scihub.copernicus.eu/dhus/odata/v1/Products(\'9faebdb4-5697-4070-
 aafe-2c2d1ee99a06\')/$value' ]

The array is the result of this expression:

 requestarray = [];
for (i=0; i<data.length;i++) {
    requestarray.push(url_search + "(" + "'" + data[i] + "'" + ")/$value");
}

Why are the "\" inserted and how could I avoid or fix it?

PhilG
  • 351
  • 1
  • 4
  • 15
  • You can use JSON.parse(array) for fix that. – Jorge Mejia Jan 22 '18 at 14:37
  • if you want to put single quote inside single quote or double quote inside double quote, you need to escape it. e.g. `'\''` or `"\""`. – n00dl3 Jan 22 '18 at 14:37
  • 3
    `['...'...']` would be invalid code. It *requires* a backslash. Whatever you're using to look at this array, it's trying to give you a valid code representation. – deceze Jan 22 '18 at 14:37
  • 2
    The backslash is not really there. It just looks like it's inside the single quotes because the single quotes are backslash escaped. e.g.: `\'phil\'` I suppose it looks like there's a backslash after the last letter of `phil` inside a single quoted string. But really it's just that the single quote that follows is escaped -- as is the initial single quote that sets off the UUID portion of that text. – Wyck Jan 22 '18 at 14:40
  • Hi, does this mean, if i send the urls in a request, the '\' would not be recognized? – PhilG Jan 22 '18 at 14:56
  • It won't be "not recognised", it's *not there*. – deceze Jan 22 '18 at 14:57

2 Answers2

0

My console happens to prefer to show me strings in double quotes:

console demo

I enter "'", it responds with the same value.

I enter '"', it responds with "\"". Both represent a string with length of 1 containing one double quote; but the one the console shows me is escaped with a backslash. That's because the console gives valid literal representations, and if the string is quoted with double quotes, any double quotes within it must be escaped.

But this is purely presentational, the string does not actually contain the backslash.

deceze
  • 510,633
  • 85
  • 743
  • 889
-2

The 'why' is because your strings contain single quotes, therefore the quotes within the final string are escaped by a \.

In your case you could avoid it because you put in the single quotes yourself, you might put in double quotes.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Jos
  • 422
  • 4
  • 10
  • 2
    The backslashes aren't really there. You can iterate the string character by character and not find a backslash in it. Therefore it also can't be avoided. It's just for presentation. – deceze Jan 22 '18 at 14:44