0

How can I replace this char \\ by \ by using javascript or jQuery? I tried this:

var string = "ABC\\testUserName"
var newString = string.replace("\\", "\")

I get this error:

Uncaught SyntaxError: Unexpected token ILLEGAL

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

2

The \ is the escape character, so you need to double them in your replace() call for them to be taken as a single instance. Try this:

var string = "ABC\\testUserName"
var newString = string.replace("\\\\", "\\");
console.log(newString);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339