简体   繁体   English

\ r和\ n之间的区别

[英]Difference between \r and \n

What is the difference between \\r and \\n in a regular expression? \\n则表达式中\\r\\n有什么区别?

Can someone explain it with an example? 有人能用一个例子解释一下吗?

\\r is "Carriage Return" (CR, ASCII character 13), \\n is "Line Feed" (LF, ASCII character 10). \\r是“回车”(CR,ASCII字符13), \\n是“换行”(LF,ASCII字符10)。 Back in the days, you had two ASCII characters at the end of each line to tell a printer what to do - CR would tell the printer to go back to the left edge of the paper, LF would advance to the next line. 在这些日子里,你在每行末尾都有两个ASCII字符告诉打印机要做什么 - CR会告诉打印机返回到纸张的左边缘,LF会前进到下一行。

Operating systems still have different conventions as to what the end of a line looks like -- some of them have \\n\\r , some have \\n , some have \\r\\n . 操作系统对于行的结尾看起来仍然有不同的约定 - 其中一些有\\n\\r ,有些有\\n ,有些有\\r\\n

In Javascript, you mostly deal with \\n - this is how strings are typically switching to the next line. 在Javascript中,你主要处理\\n - 这是字符串通常切换到下一行的方式。 However, depending on what strings you are working with, you may be encountering \\r as well. 但是,根据您使用的字符串,您可能也会遇到\\r What exactly are you doing? 你究竟在做什么?

Normally \\r represents a carriage return character (ASCII 0x0d ), and \\n is a newline character (ASCII 0x0a ). 通常\\r表示回车符(ASCII 0x0d ), \\n是换行符(ASCII 0x0a )。 This page has a list of all the special characters, quoted here for completeness: 此页面包含所有特殊字符的列表,此处引用的是完整性:

  • \\f matches form-feed. \\f匹配换页。
  • \\r matches carriage return. \\r匹配回车。
  • \\n matches linefeed. \\n匹配换行符。
  • \\t matches horizontal tab. \\t匹配水平标签。
  • \\v matches vertical tab. \\v匹配垂直选项卡。
  • \\0 matches NUL character. \\0匹配NUL字符。
  • [\\b] matches backspace. [\\b]匹配退格。
  • \\s matches whitespace (short for [\\f\\n\\r\\t\\v\ \
\
] ). \\s匹配空格( [\\f\\n\\r\\t\\v\ \
\
] )。
  • \\S matches anything but a whitespace (short for [^\\f\\n\\r\\t\\v\ \
\
] ). \\S匹配除空白之外的任何内容( [^\\f\\n\\r\\t\\v\ \
\
] )。
  • \\w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_] ). \\w匹配任何字母数字字符(单词字符),包括下划线( [a-zA-Z0-9_] )。
  • \\W matches any non-word characters (short for [^a-zA-Z0-9_] ). \\W匹配任何非单词字符( [^a-zA-Z0-9_] )。
  • \\d matches any digit (short for [0-9] ). \\d匹配任何数字( [0-9]缩写)。
  • \\D matches any non-digit (short for [^0-9] ). \\D匹配任何非数字( [^0-9]缩写)。
  • \\b matches a word boundary (the position between a word and a space). \\b匹配单词边界(单词和空格之间的位置)。
  • \\B matches a non-word boundary (short for [^\\b] ). \\B匹配非字边界( [^\\b]缩写)。
  • \\cX matches a control character. \\cX匹配控制字符。 Eg: \\cm matches control-M . 例如: \\cm匹配control-M
  • \\xhh matches the character with two characters of hexadecimal code hh . \\xhh将字符与十六进制代码的两个字符hh匹配。
  • \\uhhhh matches the Unicode character with four characters of hexadecimal code hhhh . \\uhhhh将Unicode字符与十六进制代码的四个字符hhhh匹配。

\\n is linefeed \\n是换行

\\r is carriage return \\r是回车

In windows, for example, line endings are \\r\\n . 例如,在Windows中,行结尾是\\r\\n In the vast majority of other operating systems, they are \\n . 在绝大多数其他操作系统中,它们都是\\n

\\r and \\n are digital representations of the way you would advance to the next line on a typewriter. \\r\\n是您前往打字机下一行的方式的数字表示。 \\r is a carriage return and \\n is a newline (also known as a linefeed). \\r回车符 \\n换行符 (也称为换行符)。 On a typewriter, to go to the start of the new line, you would return the carriage to the leftmost position and then feed the paper up a line. 打字机上,转到新行的开始,你会回车到最左边的位置,然后把调高一行。

Unix uses \\n to mean new line, Macs prior to OS9 used \\r , and Windows uses \\r\\n . UNIX使用\\n意味着新的生产线,都使用Mac之前OS9 \\r ,和Windows使用\\r\\n

\\n --> For a new line \\n - >换新行

\\r --> For carriage return \\r - >用于回车

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

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