[英]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
matchesNUL
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
matchescontrol-M
. 例如:\\cm
匹配control-M
。\\xhh
matches the character with two characters of hexadecimal codehh
.\\xhh
将字符与十六进制代码的两个字符hh
匹配。\\uhhhh
matches the Unicode character with four characters of hexadecimal codehhhh
.\\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.