Javascript and backslashes replace

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

    I haven't tried this, but the following should work

    var replaced = str.replace((new RegExp("\s"),"\\s");
    

    Essentially you don't want to replace "\", you want to replace the character represented by the "\s" escape sequence.

    Unfortunately you're going to need to do this for every letter of the alphabet, every number, symbol, etc in order to cover all bases

提交回复
热议问题