19

I have the following problem:

I have a script that executes an AJAX request to a server, the server returns C:\backup\ in the preview. However, the response is "C:\\backup\\". Not really a big deal, since I just thought to replace the double slashes with single ones. I've been looking around here on stack, but I could only find how to replace single backslashes with double ones, but I need it the other way around.

Can someone help me on this matter?

Guido Visser
  • 2,209
  • 5
  • 28
  • 41

3 Answers3

34

This should do it: "C:\\backup\\".replace(/\\\\/g, '\\')

In the regular expression, a single \ must be escaped to \\, and in the replacement \ also.

[edit 2021] Maybe it's better to use template literals.

console.log(`original solution ${"C:\\backup\\".replace(/\\\\/g, '\\')}`)

// a template literal will automagically replace \\ with \
console.log(`template string without further ado ${`C:\\backup\\`}`);

// but if they are escaped themselves
console.log(`Double escaped ${`C:\\\\backup\\\\`.replace(/\\{2,}/g, '\\')}`);

// multiple escaped
console.log(`multiple escaped ${`C:\\\\\\\\backup\\\\`
  .replace(/\\{2,}/g, '\\')}`);

// don't want to replace the last \\
console.log(`not the last ${`C:\\\\\\backup\\\\`
  .replace(/\\{2,}([^\\{2,}$])/g, (a ,b) => a.slice(0,1) + b)}` );

// don't want to replace the first \\
console.log(`not the first ${`C:\\\\backup\\`.replace(/\\[^\\]$/g, '\\')}`);

// a generic tagged template to replace all multiple \\ OR //
const toSingleSlashes = (strs, ...args) => 
  strs.reduce( (a, v, i ) => 
    a.concat(args[i-1] || ``).concat(v, ``), `` )
      .replace( /(\\|\/){2,}/g, (a, b) => b );

console.log(`generic tagged template 1 ${
  toSingleSlashes`C:\\backup\\`}`);

console.log(`generic tagged template 2 ${
  toSingleSlashes`C:\\\\\\\\backup\\\\\\\\\\`}`);
  
console.log(`generic tagged template 3 ${
  toSingleSlashes`C:\\\\\\\\backup\\`}`);
  
console.log(`generic tagged template 4 ${
  toSingleSlashes`C:////////backup////`}`);

console.log(`reply to comment @chitgoks => 
"test\\\\hehehe" is by default "test\\hehehe"
so, no replacement necessary here ...`);
.as-console-wrapper {
    max-height: 100% !important;
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177
4

Best is to use regex to replace all occurrences:

C:\\backup\\".replace(/\/\//g, "/")[0]

this returns: C:\backup\

OR

use split()

"C:\\backup\\".split();

both produces your desired result

C:\backup\

console.log("using \"C:\\backup\\\".replace(/\/\//g, \"/\")")
console.log("C:\\backup\\".replace(/\/\//g, "/"));

console.log("Using \"C:\\backup\\\".split()");
console.log("C:\\backup\\".split()[0]);
xxbinxx
  • 1,527
  • 11
  • 18
  • Just wanted to note this will work with forward slash but not back slash – Sami Fouad Feb 03 '17 at 04:22
  • This will also remove any single slash 'ab\cd' --> abcd – codemirror Sep 12 '17 at 11:49
  • 1
    @codemirror Good point. But if it contains '\' then you won't see it as 'ab\cd' you'll get that as 'ab\\cd' in code. – xxbinxx Nov 20 '17 at 05:33
  • Your snippet `"C:\\backup\\".split();` does not work when testing it in the Chrome console. In addition, it would be more appropriate to write `"C:\\backup\\".split()[0]` to directly obtain the wanted string. – Pieber Nov 04 '22 at 17:36
0

I think everyone might be misleading themselves the console.log(). It will automatically display the expected result, regardless of whether or not you replace any of the characters.

const test = 'C:\\';
console.log( test ) // this will write out "C:\"
console.log( `${ test.replace( /\\{2,}/g, '\\' }` ) // this will also write out "C:\"

The string value is still single character escaped. However, the console.log is hiding that fact from you.

From what I can tell, there is no way to actually replace the \\ with \ in javascript.

the closest you can get would be to do something like this, however its still interpreted the same way

test.replace( /\\{2,}/g, '\u005C' )
Dave Maison
  • 383
  • 1
  • 13