【问题标题】:I dont get why is the correct answer the one circled in green and not the above one, what is the use of the \\ ( backslash) and \\\?我不明白为什么正确答案是绿色圆圈而不是上面的那个,\\(反斜杠)和\\\\的用途是什么?
【发布时间】:2018-01-15 10:15:45
【问题描述】:

我不明白为什么正确答案是绿色圆圈而不是上面的那个, \\ (反斜杠)和 \\\\ 有什么用?

【问题讨论】:

  • 你在任何地方看到///吗?
  • 因为这就是反斜杠在字符串文字中的工作方式。阅读documentation,或任何 JS 介绍或教程,或在 Stack Overflow 上搜索“javascript escape backslash”。

标签: javascript string escaping


【解决方案1】:

JavaScript 在字符串中使用反斜杠来转义。 如果您尝试使用 C:\Users\Name\Desktop\,则需要在每个反斜杠之前和每个引号之前添加另一个反斜杠。

console.log("The file located at \"C:\\\\Desktop\""); //correct
console.log("The file located at \"C:\\Desktop"); // wrong

//console.log("The file located at "C:\\\Desktop\""); gives error

【讨论】:

    【解决方案2】:

    只是为了在同一页面上:/是正斜杠,\是反斜杠。

    必须对反斜杠进行转义(使用反斜杠),以免它们被误认为是后面的字符的转义字符。

    【讨论】:

      【解决方案3】:

      因为我们不能直接在字符串中显示 '\' 和 ' " ',因为这些字符还有其他特定含义,例如:反斜杠、换行符等并称为转义字符。我们需要使用 '\' 来转义它们(a反斜杠)。例如,如果我们要显示:

      abc\def\\ghi 在字符串中则应写为:

      abc\\def\\\\ghi
      

      否则它将打印abcdef\ghi。我们不能在字符串中写单个 '" ' 打印它会出错它应该写成:' \" '。

      console.log("abc\def\\ghi");//wrong
      console.log("abc\\def\\\\ghi");//correct

      欲了解更多信息,请访问herehere

      【讨论】:

        猜你喜欢
        • 2019-01-28
        • 1970-01-01
        • 1970-01-01
        • 2014-04-12
        • 2020-11-12
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多