简体   繁体   English

何时在JavaScript中使用特殊序列\\ b \\ f和\\ r

[英]When to use special sequences \b \f and \r in JavaScript

I know how \\n works but I have trouble to understand these three special sequences in javascript 我知道\\n工作原理,但是我很难理解javascript中的这三个特殊序列
- \\b (backspace) -\\ b(退格键)
- \\f (form feed) -\\ f(换页)
- \\r (carriage return) -\\ r(回车)
For example for two first sequences I get as output a string with a "strange" char. 例如对于两个第一个序列,我得到一个带有“奇怪”字符的字符串作为输出。 For \\r I get testtest for string test\\rtest , so it do nothing. 对于\\r我得到testtest字符串test\\rtest ,所以什么也不做。
When to use these three escape sequences? 什么时候使用这三个转义序列?

These ASCII control characters have a long historical legacy that is probably a bit off topic for this question, but it's important to understand that they have slightly different meanings or sometimes no meaning in different contexts. 这些ASCII控制字符具有悠久的历史遗产,对于这个问题来说可能有点不合时宜,但重要的是要了解它们的含义略有不同,或者在不同的上下文中有时没有含义。

\\b - One thing to keep in mind is that characters are used for both input and output . \\b要记住的一件事是, 输入输出都使用字符。 Whereas \\b is not really useful in output , if you are reading input, you may find capturing a backspace useful in some scenarios. 尽管\\b输出中并不是真正有用,但如果您正在读取输入,则可能会发现在某些情况下捕获退格很有用。

\\f - Form feed is historically used to advance something like a printer to the next page, so it's not really relevant to most situations. \\f换页历来用于将打印机之类的内容推进到下一页,因此它实际上与大多数情况无关。 Sometimes some consoles may do something useful when they interpret these, but there's no standard behavior for modern terminal emulators. 有时,某些控制台在解释它们时可能会做一些有用的事情,但是现代终端仿真器没有标准行为。

\\r - The carriage return is still important for some operating systems (especially Windows) because newline ( \\n ) has not always had an implied carriage return associated with it. \\r对于某些操作系统(尤其是Windows),回车符仍然很重要,因为换行符( \\n )并不总是具有与其关联的隐式回车符。 What this means for modern terminals is that Windows, when getting a \\n moves the cursor down a row, but not back to the first column. 这对于现代终端意味着Windows在获得\\n时将光标向下移动一行,但不返回第一列。 Thus, end-of-lines have historically been the sequence \\r\\n so that you both advance to the next line and return to column 1. Unix systems generally use just \\n for both making \\r obsolete. 因此,行尾历来是序列\\r\\n因此您都可以前进到下一行并返回第1列。Unix系统通常都只使用\\n来使\\r过时。 In Node, you can use os.EOL to get the appropriate string to use for end-of-lines. 在Node中,您可以使用os.EOL获得适当的字符串以用于行尾。

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

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