0

How can I replace in my string \\ to \? For example, I want to convert RegExp('\\\\b') to RegExp('\\b'). I've tried:

 mystring.replace('\\','\'');
Amruth
  • 5,792
  • 2
  • 28
  • 41
Tiago Castro
  • 421
  • 6
  • 20

1 Answers1

3

If you need to replace all of the occurrences of two backslashes in a row into a single backslash, you use a regular expression with the g flag. Because backslashes are special in regular expressions, you have to escape them (with another backslash). You also have to use the return value of replace:

var str = "Here: \\\\ and here \\\\";
console.log(str);
str = str.replace(/\\\\/g, "\\");
console.log(str);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875