How do I add slashes to a string in Javascript?

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

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

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 19:43

    An answer you didn't ask for that may be helpful, if you're doing the replacement in preparation for sending the string into alert() -- or anything else where a single quote character might trip you up.

    str.replace("'",'\x27')
    

    That will replace all single quotes with the hex code for single quote.

提交回复
热议问题