Lee
Lee

Reputation: 1171

JQuery remove backslash with some words in string

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);

Upvotes: 0

Views: 194

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

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);

Upvotes: 2

mrid
mrid

Reputation: 5796

You need to escape \

  var filePath = "\\upload\\document\\file.txt";
  filePath= filePath.replace('\\upload', '');
      
  console.log(filePath)

Upvotes: 1

Related Questions