here is my string:
var str = "This is my \string";
This is my code:
var replaced = str.replace("/\\/", "\\\\");
I can't get my output to be:
"This is my \\string"
I have tried every combination I can think of for the regular expression and the replacement value.
Any help is appreciated!
To remove all backslashes from a string:Call the replaceAll method, passing it a string containing 2 backslashes as the first parameter and an empty string as the second - str. replaceAll('\\', '') . The replaceAll method returns a new string with all of the matches replaced.
Try: string. replace(/\\\//g, "/");
replaceAll("\\/", "/");
Got stumped by this for ages and all the answers kept insisting that the source string needs to already have escaped backslashes in it ... which isn't always the case.
Do this ..
var replaced = str.replace(String.fromCharCode(92),String.fromCharCode(92,92));
The string doesn't contain a backslash, it contains the \s
escape sequence.
var str = "This is my \\string";
And if you want a regular expression, you should have a regular expression, not a string.
var replaced = str.replace(/\\/, "\\\\");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With