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

前端 未结 4 1390
礼貌的吻别
礼貌的吻别 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:24

    Is it possible, in javascript, to declare a string with information inside of it, that does not use single or double quotes?

    No. JavaScript string literals are delimited with either single quotes or double quotes. Those are the only choices.

    does anyone know a simple and easy way to escape these quotes so I can put it in a string?

    Do you mean in a string literal?

    var str = "var foo=\"bar\"; function say(it) { alert('It is: ' + it); say(foo);";
    

    Or programmatically?

    str = str.replace(/"/g, '\\"');
    

提交回复
热议问题