简体   繁体   English

Javascript:回车如何工作?

[英]Javascript: How does the carriage return work?

I am going through the Javascript course on FreeCodeCamp.com and am stuck at this challenge, 'Escape Sequences in Strings': 我正在FreeCodeCamp.com上学习Java语言课程,并且陷入了“字符串中的转义序列”这一难题:

Assign the following three lines of text into the single variable myStr using escape sequences. 使用转义序列将以下三行文本分配到单个变量myStr

 FirstLine \\SecondLine\\ ThirdLine 

You will need to use escape sequences to insert special characters correctly. 您将需要使用转义序列正确插入特殊字符。 You will also need to follow the spacing as it looks above, with no spaces between escape sequences or words. 您还需要遵循上面的空格,转义序列或单词之间没有空格。

Here is the text with the escape sequences written out. 这是写有转义序列的文本。

"FirstLine newline backslash SecondLine backslash carriage-return ThirdLine" “第一行换行反斜线第二行反斜线回车第三行”

Here is how I'm writing the code example: 这是我编写代码示例的方式:

var myStr = "Firstline\n\\Secondline\\\rThirdline";

The check requires there to be only one \\n . 检查只需要一个\\n The \\r doesn't seem to function properly. \\r似乎无法正常运行。 I have tried different work arounds and searching hasn't provided any direction. 我尝试了不同的解决方法,但搜索没有提供任何方向。 I have tried using 'console' in the developer window of the browser and it appears \\n is working but the \\r is not. 我曾尝试在浏览器的开发人员窗口中使用“控制台”,但看来\\n正在工作,但\\r无法工作。

  1. Is the carriage-return not working? 回车不起作用吗?
  2. Am I misunderstanding the lesson? 我对这节课有误解吗?

据我了解,回车符是在换行符之前,因此我认为必须使用/r/nThirdline才能使回车符/r/nThirdline

According to the book 'Javascript Bible' by Danny Goodman, chapter 15 'The String Objects', section 'Special Inline Characters': 根据丹尼·古德曼(Danny Goodman)的《 Javascript Bible》一书中第15章“字符串对象”的“特殊内联字符”部分:

"In most cases, browsers ignore carriage returns or render them like spaces." “在大多数情况下,浏览器会忽略回车符或将它们呈现为空格。”

https://books.google.com/books?id=WMr2NEG88icC&pg=PT262&lpg=PT262&dq=javascript+carriage+return&source=bl&ots=dWFLqN0rlO&sig=eZGo_74AHZq_4e2pxmtiqXz6l9s&hl=en&sa=X&ved=0ahUKEwivlMaEqu_QAhWBOSYKHSviBM44HhDoAQgjMAI#v=onepage&q=javascript%20carriage%20return&f=false https://books.google.com/books?id=WMr2NEG88icC&pg=PT262&lpg=PT262&dq=javascript+carriage+return&source=bl&ots=dWFLqN0rlO&sig=eZGo_74AHZq_4e2pxmtiqXz6l9s&hl=en&sa=X&ved=0ahUKEwivlMaEqu_QAhWBOSYKHSviBM44HhDoAQgjMAI#v=onepage&q=javascript%20carriage%20return&f=false

This might be a bug in how FreeCodeCamp checks this particular lesson. 这可能是FreeCodeCamp如何检查此特定课程的错误。 The code is probably rendered in the browser and so the check will always fail. 代码可能在浏览器中呈现,因此检查将始终失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM