var a="a\a\a/b"
var reg=/\/g;
alert(a.replace(reg,"-"));
The final output result of my code is aa-a/b
The regex only replaces double backslashes, but not single backslashes. How can I modify it to replace it?
The reason is because \ is used as an escape character. You can see in the chrome console that the final output of "aa\a/b" is "aaa/b"
How to solve this situation?
The backslash "" is an escape character. It appears in front of a character and represents a whole. For example, "n" represents a newline character. See the code below:
In other words, "f" counts as one character.
Hope this helps!
I checked the information and found no solution. It is regarded as an escape character, which is a low-level implementation and cannot be searched and replaced. The characters still have to be written as
"a\a\\a/b"
What kind of scenario do you want to use it for? I don’t know if this situation is the result you want:
Create
test.txt
, the content isaa\a/b
Create
test.js
, and perform some tests and results on the node console below:Then I think it makes no sense to replace
var a="aa\a/b"
the backslash of thisstring
. You may have confusedstring
andtext character