How to escape backslash in JavaScript?

open console and type

'\'.replace(/\\/g, '\'); 

fails because the slash in the string isn’t really in the string, it’s escaping ‘

'\\'.replace(/\\/g, '\');

works because it takes one slash and finds it.

your regex works.

Leave a Comment