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('\\'); ["", "folderolder1older2older3anotherFolderilname.pdf"]
3
  • The first string has a single backslash in it - at the beginning. \f is just an escaped character f. Commented Sep 27, 2022 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: unicode-table.com/en/000C Commented Sep 27, 2022 at 13:41
  • Take a look at stackoverflow.com/questions/2479309/… Commented Sep 27, 2022 at 13:42

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.