Javascript and backslashes replace

前端 未结 7 1530
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 07:50

here is my string:

var str = \"This is my \\string\";

This is my code:

var replaced = str.replace(\"/\\\\/\", \"\\\\\\\\\")         


        
7条回答
  •  爱一瞬间的悲伤
    2020-11-27 08:45

    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(/\\/, "\\\\");
    

提交回复
热议问题