Special character replacement

I want to replace the escape character (\) in the path to a single \ in js

\\cpmp1\D$\\ABC\\XYZ 

I tried

 console.log(s.replace(/.\\\\/g, '\\')); 

but he does not replace any ideas?

+6
source share
2 answers
 var replacedStr = s.replace(/\\+/g, '\\'); 
+4
source

You do not need a ".", But otherwise it will work.

 console.log(s.replace(/\\\\/g, '\\')); 

If this does something wrong, I suggest that your testing method might have a problem with it.

edit - Note that to verify this with the suggested source string, you must double the backslash:

 var s = "\\\\cpmp1\\D$\\\\ABC\\\\XYZ"; 
+2
source

Source: https://habr.com/ru/post/901439/


All Articles