How do I escape backslash in java script . This is what I need: ( tried from browser developer console)
var str = 'this is \\ test';
console.log(str);
This works fine and prints: this is \ test
How do I do the same using replace function . Basically the above hard code (\) works . I want to have a generic solution for any string .
I tried below:
var str = 'this is \ test';
str=str.replace(/\\/g, "\\\\");
console.log(str);
but it just prints : this is test
My end goal is to pass a string with backslash in a REST Json Body . However it fails with HTTP 400 bad request . So I am looking to escape the backslash before i pass it on to the Json body
So I am looking for a code to replace \ with \