Pawan Kumar
Pawan Kumar

Reputation: 1374

Backslashes are getting removed from string

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?

Upvotes: 0

Views: 1926

Answers (2)

Polina
Polina

Reputation: 1

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.

Upvotes: 0

huysentruitw
huysentruitw

Reputation: 28111

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.

Upvotes: 2

Related Questions