How can I accommodate a string with both single and double quotes inside of it in JavaScript

前端 未结 4 1400
礼貌的吻别
礼貌的吻别 2020-12-03 15:12

I have an application, and it is fed some HTML. It then needs to put that HTML into a string. This HTML contains single and double quotes. Is it possible, in javascript, to

4条回答
  •  感动是毒
    2020-12-03 15:37

    Use the escape function to replace special characters (including single and double quotes). You can then use the unescape function to return the string to it's normal state later if necessary.

    For example:

    var data = 'hello my name is "James"';
    alert(escape(data)); //Outputs: hello%20my%20name%20is%20%22James%22
    

提交回复
热议问题