0

Javascript removes back slashes from string.

var a='https:\\abc\qwe.com';
console.log(a);
var b=encodeURI(a);
console.log(b);
var c='https:\\\abc\\qwe.com';
console.log(c);
var d='https:\\\\abc\\qwe.com';
console.log(d);

Is there any way to get Javascript to not remove the backslashes in the console.log input strings?

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
Pawan Kumar
  • 1,374
  • 7
  • 14

2 Answers2

2

In JavaScript (and other languages) the backslash \ is used as an escape character, f.e. to be able to get a double quote in your string: "\"". (Read more here under Escape notation)

The side effect is that, if you want a backslash in your string, you need a double backslash: "\\" results in \.

This said, URL's use slashes instead of backslashes: "https://abc/qwe"

If you're looking for URL encoding, use the encodeURI or encodeURIComponent function.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
0

You are using backslashes, not slashes(/). They are used to escape special symbols (and the backslash itself). You do not need them to write URLs, actually.

Polina
  • 1
  • 1