Javascript replace “ ' ” with “ '' ”

后端 未结 7 737
陌清茗
陌清茗 2021-01-22 12:24

I\'m trying to replace the \"\'\" character with the \"\'\'\" string using the replace method, like this:

temp.replace(\"\\\'\", \"\'\'\");

but

7条回答
  •  我在风中等你
    2021-01-22 12:37

    The problem is that

    temp.replace("\'", "''");
    

    will only replace the first instance of '. To fix this, do the following instead

    temp.replace(/'/g, "''"));
    

    This will ensure it goes though and replaces all instances of the single quote instead of just the first.

提交回复
热议问题