Javascript replace “ ' ” with “ '' ”

后端 未结 7 676
陌清茗
陌清茗 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:57

    Your code just replaces a single instance (the first one it finds). You should replace all instances. You can do this by using a regular expression and adding a g flag to the end meaning "global search". Like this:

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

    Here's a working example: http://jsfiddle.net/Q2Uyv/ (type something into the "In" box and click "Convert").

    If you are actually trying to replace single quotes with double quotes (instead of doubled sequence of single quotes), do this:

    temp.replace(/'/g, '"')
    
    0 讨论(0)
提交回复
热议问题