The character '\' is used as an escape character.
So when you include it in a string the next char is the one that is considered what you are after.
So str.replace("\","\\") means if you find 'nothing/empty string' replace it with one '\'.
You need something like the following
I think the second is the better approach
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.