Replace single backslash “\” with double backslashes “\\”

I have string with file path. I want to replace all single backslashes (“\”) with double backslashes (“\\”).

   var replaceableString = "c:\asd\flkj\klsd\ffjkl";
   var part = /@"\\"/g;
   var filePath = replaceableString .replace(part, /@"\\"/);

Console showed me it.


I found something like this, unfortunately it didn’t work.
Replacing \ with \\


   var parts = replaceableString.split('\\');
   var output = parts.join('\\\\');

Personally, as I am not so expert in reg exps, I tend to avoid them when dealing with non-alphanumeric characters, both due to readability and to avoid weird mistake.

var replaceableString = "c:\asd\flkj\klsd\ffjkl";

This will alert you c:asdlkjklsdfjkl because ‘\’ is an escape character which will not be considered.

To have a backslash in your string , you should do something like this..

var replaceableString = "c:\\asd\\flkj\\klsd\\ffjkl";

This will alert you c:\asd\flkj\klsd\ffjkl

JS Fiddle

Learn about Escape sequences here

If you want your string to have ‘\’ by default , you should escape it .. Use escape() function

var replaceableString = escape("c:\asd\flkj\klsd\ffjkl");

JS Fiddle

You have several problems in your code.

  1. To get a \ in your string variable you need to escape it.

    When you create a string like this: replaceableString = "c:\asd\flkj\klsd\ffjkl"; characters with a \ before are treated as escape sequences. So during the string creation, it tries to interpret the escape sequence \a, since this is not valid it stores the a to the string. E.g. \n would have been interpreted as newline.

  2. I assume the @ is coming from a .net example. Javascript does not know “raw” strings.

  3. remove the quotes from your regex.

Read More:   Opposite of Object.freeze or Object.seal in JavaScript

This would do what you want:

var string = "c:\\asd\\flkj\\klsd\\ffjkl";
var regex = /\\/g;
var FilePath = string.replace(regex, "\\\\");

Here is the answer:

For replacing single backslash with single forward slash:

var stringReplaced = String.raw`c:\asd\flkj\klsd\ffjkl`.split('\\').join("")

For replacing double backslash with single forward slash:

var stringReplaced = String.raw`c:\\asd\\flkj\\klsd\\ffjkl`.split('\\\\').join("")

\ is a escape character. Therefore replaceableString does not contain any backslashes.

To fix this you should declare the string like this:

var replaceableString = "c:\\asd\\flkj\\klsd\\ffjkl";

First encode the string

then replace all occurrences of %5C with %5C%5C

At the end decode the string

var result = encodeURI(input);

If you have no control over the contents of the string you are trying to find backslashes in, and it contains SINGLE \ values (eg. variable myPath contains C:\Some\Folder\file.jpg), then you can actually reference the single backslashes in JavaScript as String.fromCharCode(92).

So to get the file name in my filepath example above.

var justTheName = myPath.split(String.fromCharCode(92)).pop();

In case of string matching, it is better to use encodeURIComponent, decodeURIComponent.


function match(input)

for(i=0; i<arr.length; i++)
if(arr[i] == decodeURIComponent(input))
return true;
else return false;

In the case of a single back slash in the string, the javascript replace method did not allow me to replace the single back slash.

Instead I had to use the split method which returns an array of the split strings and then concatenate the strings without the back slash (or whatever you want to replace it with)

Read More:   How to convert BigInt to Number in JavaScript?

Solution (replaced backslash with underscore):

var splitText = stringWithBackslash.split('\\');
var updatedText = splitText[0] + '_' + splitText[1];

You need to pass to pass value of a string through String.raw before you assign value to a variable.

var replaceableString = String.raw`c:\asd\flkj\klsd\ffjkl`.replace(/\\/g,"\\\\");

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts