user2049371
user2049371

Reputation: 199

how to replace backslash character with double backslash in javascript?

I'm trying to replace backslashes in my string with two backslashes like so:

s = s.replace("\\", "\\\\");

But, it doesn't do anything. Example string:

s="\r\nHi\r\n";

Upvotes: 2

Views: 2236

Answers (1)

Naveen Kumar Alone
Naveen Kumar Alone

Reputation: 7668

The string doesn't contain a backslash, it contains the \r escape sequence.

Working example

For example

var str = "\r\n";
var replaced = str.replace('\r\n', '\\r\\n');
alert(replaced);

Then the alert will be shown \r\n

Upvotes: 2

Related Questions