30

What is the difference between \r and \n in a regular expression?

Can someone explain it with an example?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123

5 Answers5

52

\r is "Carriage Return" (CR, ASCII character 13), \n is "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.

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.

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 \r as well. What exactly are you doing?

HamZa
  • 13,530
  • 11
  • 51
  • 70
EboMike
  • 72,990
  • 14
  • 152
  • 157
  • 4
    I don't know of any platform or protocol that specifies `\n\r` as the new line function; the native line separator on MAC OS prior to OS X was `\r` alone. – Alan Moore Aug 11 '10 at 02:34
  • @Alan If you want us to tell you, just ask. :) – bzlm Oct 17 '10 at 16:55
  • @bzim: You mean you know of a system that uses `\n\r` (`LFCR`, `0x0A 0x0D`) as a line separator? Do tell! – Alan Moore Oct 18 '10 at 02:12
  • This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Escape Sequences". – aliteralmind Apr 10 '14 at 01:03
22

Normally \r represents a carriage return character (ASCII 0x0d), and \n is a newline character (ASCII 0x0a). This page has a list of all the special characters, quoted here for completeness:

  • \f matches form-feed.
  • \r matches carriage return.
  • \n matches linefeed.
  • \t matches horizontal tab.
  • \v matches vertical tab.
  • \0 matches NUL character.
  • [\b] matches backspace.
  • \s matches whitespace (short for [\f\n\r\t\v\u00A0\u2028\u2029]).
  • \S matches anything but a whitespace (short for [^\f\n\r\t\v\u00A0\u2028\u2029]).
  • \w matches any alphanumerical character (word characters) including underscore (short for [a-zA-Z0-9_]).
  • \W matches any non-word characters (short for [^a-zA-Z0-9_]).
  • \d matches any digit (short for [0-9]).
  • \D matches any non-digit (short for [^0-9]).
  • \b matches a word boundary (the position between a word and a space).
  • \B matches a non-word boundary (short for [^\b]).
  • \cX matches a control character. E.g: \cm matches control-M.
  • \xhh matches the character with two characters of hexadecimal code hh.
  • \uhhhh matches the Unicode character with four characters of hexadecimal code hhhh.
Carl Norum
  • 201,810
  • 27
  • 390
  • 454
  • 3
    Think of an old manual type writer ... carriage return moves the current 'position' back to the start of the line. – StuartLC Aug 10 '10 at 16:32
  • 2
    @Avinash, @nonnb is correct - carriage return and line feed are terms back from the days of paper terminals; the carriage return character moves the printing head back to column zero, and line feed advances the paper roll by one line. – Carl Norum Aug 10 '10 at 16:34
  • `\0` doesn't belong there. `\{number}` is just an octal escape sequence. – Eli Grey Aug 10 '10 at 17:33
8

\n is linefeed

\r is carriage return

In windows, for example, line endings are \r\n. In the vast majority of other operating systems, they are \n.

HamZa
  • 13,530
  • 11
  • 51
  • 70
Jhong
  • 2,584
  • 18
  • 19
5

\r and \n are digital representations of the way you would advance to the next line on a typewriter. \r is a carriage return and \n is a newline (also known as a linefeed). 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.

Daniel Vandersluis
  • 83,484
  • 18
  • 156
  • 151
3

\n --> For a new line

\r --> For carriage return

HamZa
  • 13,530
  • 11
  • 51
  • 70
Sreeja
  • 151
  • 11