【问题标题】:Javascript - How to add strings with quotation marks inside a quotation mark?Javascript - 如何在引号内添加带引号的字符串?
【发布时间】:2023-01-13 11:05:06
【问题描述】:

我不确定这个问题的最佳表达方式是什么,但基本上我正在开发我的第二个 Javascript 项目来构建一个随机引用生成器,当我注意到我的第三个引用(请参阅第一个屏幕截图)上存在一些语法问题时,我的字符串包括诸如“不要”或“不能”之类的词。我不确定是不是因为我把我的字符串写成“quote”——作者,但是 Youtube 上的这个人(请看第二张截图)用相同的格式写了他的字符串,但没有任何语法问题。

Screenshot1

Screenshot2

【问题讨论】:

标签: javascript string quotation-marks


【解决方案1】:

以上所有行在 JavaScript 中都是允许的:

const s1 = "a 'test' a";
const s2 = "a "test" a";
const s3 = 'a 'test' a';
const s4 = 'a "test" a';
const s5 = `a 'test' a`;
const s6 = `a "test" a`;

【讨论】:

    【解决方案2】:

    使用转义字符 () “然后他说,‘你好,世界!’”

    document.write("Then he said, "Hello, World!"");
    

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多