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));
Answer from thegajman on Stack OverflowCan't replace backslash in a string with regex
php - How to remove backslash from a string in JavaScript - Stack Overflow
html - Removing backslashes from strings in javascript - Stack Overflow
How to replace backslash in string using javascript? - Stack Overflow
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(/\\/, "\\\\");
I need to replace all backslashes in a string with another symbol. I use regex for that, but it just doesn't work
const name = 'AC\DC'; const replaced = icon.replace(/\\/g, "-"); console.log(name); console.log(replaced); // ACDC // ACDC
For example in this string the backslash is not replaced and not even shown when I log the origial string in a console.
You've said what you quoted above is a string, but it's unclear whether you mean this (shortened a bit):
var str = "<tr><td><span class=\'label label-info\'>Dialed...";
...where what you've quoted is what you literally have within quotes, or this (note the backslashes):
var str = "<tr><td><span class=\\'label label-info\\'>Dialed...";
...where what you've quoted is the actual content of the string, not part of a string literal.
The first one above doesn't have any backslashes in it, it has escaped ' characters. The second one has backslashes in it.
To remove the backslahes from the second one:
str = str.replace(/\\/g, "");
When you give a regular expression with the g flag to replace, it applies globally throughout the string. Backslashes have special meaning in regular expressions, and so I've had to escape the backslash (with another backslash, it's the escape character for regular expressions as well as strings). So in the above, I'm saying to replace all backslashes with an empty string.
you can use RegExp to find backslash in JS string like this:
string.eplace(/\\\//g, "/");
demo
Andrew Cooper's answer is correct in terms of why that third statement is going wrong. But you're also overwriting newbigbend each time, so you won't see the result of the first two replacements at all.
If you're trying to replace all slashes, backslashes, and asterisks with nothing, do this:
newbigbend = bb_val.replace(/[/\\*]/g, "");
Note you don't need the i flag, none of those characters is case sensitive anyway. (And note that within the [], you don't need to escape / or *, because they don't have special meaning there.) Live example.
But if you want it as three individual statements for whatever reason, then use newbigbend in the second two (and add the backslash Andrew flagged up):
newbigbend = bb_val.replace(/\//gi,"");
newbigbend = newbigbend.replace(/\\/gi,"");
newbigbend = newbigbend.replace(/\*/gi,"");
You also need to escape the *
newbigbend = bb_val.replace(/\*/gi,"");
You don't need to make a regex a string, and it helps having that first / in there
Path = Path.replace(/\\/g, "|")
The correct syntax would be: Path = Path.replace(/\\/g, "|");
Working example at: http://jsfiddle.net/eDKej/.
Example (extra code for demonstration purposes only):
var Path = $("#path").text();
Path = Path.replace(/\\/g, "|");
$("#new-path").append(Path);
So, I'm getting the below when I parse the example input and print it in javascript. I'm not sure how to fix this.
.|....... |.-...... .....|-... ........|. .......... ......... ..../.\.. .-.-/..|.. .|....-|. ..//.|....