0
var myString = '\\folder\folder1\folder2\folder3\anotherFolder\filname.pdf';

The goal is to get

myString = '\\\\folder\\folder1\\folder2\\folder3\\anotherFolder';

My first idea was to split on each backslash, do a loop to create the new string. I got this result for the split

myString.split('\\'); ["", "folderolder1older2older3anotherFolderilname.pdf"]
Chris
  • 927
  • 1
  • 8
  • 13
  • The first string has a single backslash in it - at the beginning. `\f` is just an escaped character `f`. – VLAZ Sep 27 '22 at 13:40
  • 1
    There is only on backslash in the string literal. `\f` is a single character. `'\f'.length === 1` and `'\f'.charCodeAt(0)` it returns 12, the Form Feed character: https://unicode-table.com/en/000C/ – adiga Sep 27 '22 at 13:41
  • Take a look at https://stackoverflow.com/questions/2479309/javascript-and-backslashes-replace – klediooo Sep 27 '22 at 13:42

1 Answers1

0
var myString = 
String.raw`\\folder\folder1\folder2\folder3\anotherFolder\filname.pdf`;
var mySplit = myString.split('\\');
var result;

for (var i =0; i<(mySplit.length-1);i++){
    result = result +'\\'+mySplit[i];

}

result = result.replace('undefined','\\');

Result is

"\\\\folder\folder1\folder2\folder3\anotherFolder"
Chris
  • 927
  • 1
  • 8
  • 13