How do I add slashes to a string in Javascript?

后端 未结 8 1723
陌清茗
陌清茗 2020-12-29 19:05

Just a string. Add \\\' to it every time there is a single quote.

8条回答
  •  隐瞒了意图╮
    2020-12-29 19:49

    var str = "This is a single quote: ' and so is this: '";
    console.log(str);
    
    var replaced = str.replace(/'/g, "\\'");
    console.log(replaced);
    

    Gives you:

    This is a single quote: ' and so is this: '
    This is a single quote: \' and so is this: \'
    

提交回复
热议问题