0

my string is "\upload\document\file.txt", i want to remove the "\upload" part in that string with jQuery. I try this one but it does not work:

var filePath = "\upload\document\file.txt";
filePath = filePath.replace('\upload', '');
console.log(filePath);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Lee
  • 1,041
  • 7
  • 20
  • 31
  • 1
    Possible duplicate of [JavaScript backslash (\‌) in variables is causing an error](https://stackoverflow.com/questions/3903488/javascript-backslash-in-variables-is-causing-an-error) – George Nov 15 '17 at 16:36

2 Answers2

2

The issue is because the \u at the start of the string is being interpreted as Unicode. Hence the error you see in the console.

To avoid this problem you need to use \\ to escape the single slashes in the string:

var filePath = "\\upload\\document\\file.txt";
filePath = filePath.replace('\\upload', '');
console.log(filePath);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You need to escape \

  var filePath = "\\upload\\document\\file.txt";
  filePath= filePath.replace('\\upload', '');
      
  console.log(filePath)
mrid
  • 5,782
  • 5
  • 28
  • 71