Javascript and backslashes replace

前端 未结 7 1512
伪装坚强ぢ
伪装坚强ぢ 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:30

    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));
    

提交回复
热议问题