Javascript \r 和 \n 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3451147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between \r and \n
提问by EboMike
What is the difference between \rand \nin a regular expression?
正则表达式中\r和之间有什么区别\n?
Can someone explain it with an example?
有人可以用一个例子来解释吗?
回答by EboMike
\ris "Carriage Return" (CR, ASCII character 13), \nis "Line Feed" (LF, ASCII character 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.
\r是“回车”(CR,ASCII 字符 13),\n是“换行符”(LF,ASCII 字符 10)。过去,每行末尾有两个 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. However, depending on what strings you are working with, you may be encountering \ras well. What exactly are you doing?
在 Javascript 中,您主要处理\n- 这就是字符串通常切换到下一行的方式。但是,根据您使用的字符串,您也可能会遇到\r。你到底在做什么?
回答by Carl Norum
Normally \rrepresents a carriage return character (ASCII 0x0d), and \nis a newline character (ASCII 0x0a). This pagehas a list of all the special characters, quoted here for completeness:
通常\r表示回车符 (ASCII 0x0d),并且\n是换行符 (ASCII 0x0a)。 此页面包含所有特殊字符的列表,为了完整起见,在此引用:
\fmatches form-feed.\rmatches carriage return.\nmatches linefeed.\tmatches horizontal tab.\vmatches vertical tab.\0matchesNULcharacter.[\b]matches backspace.\smatches whitespace (short for[\f\n\r\t\v\u00A0\u2028\u2029]).\Smatches anything but a whitespace (short for[^\f\n\r\t\v\u00A0\u2028\u2029]).\wmatches any alphanumerical character (word characters) including underscore (short for[a-zA-Z0-9_]).\Wmatches any non-word characters (short for[^a-zA-Z0-9_]).\dmatches any digit (short for[0-9]).\Dmatches any non-digit (short for[^0-9]).\bmatches a word boundary (the position between a word and a space).\Bmatches a non-word boundary (short for[^\b]).\cXmatches a control character. E.g:\cmmatchescontrol-M.\xhhmatches the character with two characters of hexadecimal codehh.\uhhhhmatches the Unicode character with four characters of hexadecimal codehhhh.
\f匹配换页。\r匹配回车。\n匹配换行符。\t匹配水平制表符。\v匹配垂直制表符。\0匹配NUL字符。[\b]匹配退格。\s匹配空格( 的缩写[\f\n\r\t\v\u00A0\u2028\u2029])。\S匹配除空格( 的缩写[^\f\n\r\t\v\u00A0\u2028\u2029])以外的任何内容 。\w匹配任何字母数字字符(单词字符),包括下划线( 的缩写[a-zA-Z0-9_])。\W匹配任何非单词字符( 的缩写[^a-zA-Z0-9_])。\d匹配任何数字( 的缩写[0-9])。\D匹配任何非数字( 的缩写[^0-9])。\b匹配单词边界(单词和空格之间的位置)。\B匹配非单词边界( 的缩写[^\b])。\cX匹配一个控制字符。例如:\cm匹配control-M。\xhh匹配具有两个十六进制代码字符的字符hh。\uhhhh将 Unicode 字符与四个十六进制代码字符匹配hhhh。
回答by Jhong
\nis linefeed
\n是换行
\ris carriage return
\r是回车
In windows, for example, line endings are \r\n. In the vast majority of other operating systems, they are \n.
例如,在 Windows 中,行结尾是\r\n. 在绝大多数其他操作系统中,它们是\n.
回答by Daniel Vandersluis
\rand \nare digital representations of the way you would advance to the next line on a typewriter. \ris a carriage returnand \nis a newline(also known as a linefeed). On a typewriter, to go to the start of the new line, you would returnthe carriage to the leftmost position and then feedthe paper up a line.
\r并且\n是您在打字机上前进到下一行的方式的数字表示。\r是一个回车和\n是一个新行(也称为换行)。在打字机上,要转到新行的开头,您可以将笔架返回到最左边的位置,然后将纸张向上送入一行。
Unix uses \nto mean new line, Macs prior to OS9 used \r, and Windows uses \r\n.
Unix 用于\n表示换行,OS9 之前的 Mac 使用\r,Windows 使用\r\n.
回答by Sreeja
\n--> For a new line
\n--> 换新行
\r--> For carriage return
\r--> 用于回车

