How can I replace in my string \\
to \
? For example, I want to convert RegExp('\\\\b')
to RegExp('\\b')
. I've tried:
mystring.replace('\\','\'');
How can I replace in my string \\
to \
? For example, I want to convert RegExp('\\\\b')
to RegExp('\\b')
. I've tried:
mystring.replace('\\','\'');
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);