\r matches the carriage return (CR) character (U+000D, ASCII 13). It is half of Windows \r\n (CRLF) and the whole line ending in some legacy Mac text. Pair it with \n when you parse cross-platform files.
Remember
\r → carriage return (U+000D)
\n → line feed (different character)
\r\n → Windows-style line ending (CR + LF)
\s → any whitespace, including \r
"a\rb" → CR between letters
/\r/.test(...) → true when CR is present
/\r\n|\r|\n/ → any common line ending
Use
Prefer
Detect a CR
/\r/
Windows line break
/\r\n/
Normalize to LF
replace(/\r\n|\r/g, "\n")
Split any ending
split(/\r\n|\r|\n/)
Approach
How to Use \r
Treat carriage return as a concrete character — test for it, match CRLF as a pair, or normalize mixed endings.
Create or import text that may contain CR (string escape: "\r").
Use /\r/ with test, match, split, or replace.
For Windows files, match /\r\n/ as one unit when you care about the full ending.
When normalizing, list \r\nbefore\r so CRLF does not become two replacements.
Piece
Required?
Role
\r in the pattern
Yes
Match the carriage return character
String escape "\r"
For demos / fixtures
Insert CR into a JavaScript string
g flag
Optional
Find every CR or CRLF
Alternation order
When normalizing
Put \r\n before \r
Pseudocode
Pseudocode
pattern has "\r"
read next character ch from the string
if ch.codePoint === 0x000D: // carriage return
accept match
else:
fail this path
common actions:
test(str) → true/false
match(/\r\n/g) → Windows breaks
replace(/\r\n|\r/g, "\n") → normalize to LF
split(/\r\n|\r|\n/) → lines for any style
Reference
Cheat Sheet
Keep these facts handy when working with carriage returns.
Topic
Detail
Character
Carriage return — U+000D (ASCII 13)
Regex / string
/\r/ · "\r" · new RegExp("\\r")
vs LF / CRLF
\r ≠ \n; CRLF is \r\n
Whitespace
Included in \s
Normalize
replace(/\r\n|\r/g, "\n")
Mixed split
/\r\n|\r|\n/ (list CRLF first)
Standard
ES3+ (universal)
Try it
Live Preview
Run common \r operations on sample text with CR and CRLF.
Browser demo of the RegExp \r carriage return escape — same ideas as the examples below.
Result
text = "hello\rworld"
/\r/.test(text)
→ true
/* carriage return (U+000D) is present */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Detect a Carriage Return
Return true when the text contains at least one CR.
One line:\r finds carriage returns — match \r\n for Windows breaks, and normalize with /\r\n|\r/g before assuming Unix newlines.
Frequently Asked Questions
\r matches the carriage return control character (CR, Unicode U+000D, ASCII 13). It appears in Windows CRLF (\r\n) line endings and in some legacy Mac text that used CR alone.
\r is carriage return (CR). \n is line feed (LF). Unix and modern macOS use \n alone. Windows traditionally uses both together as \r\n (CRLF).
Windows files often use CRLF (\r\n). Matching only \r can leave stray \n characters. For full Windows line breaks as one unit, use /\r\n/g. To normalize, use /\r\n|\r/g → "\n".
In a regex literal like /\r/, the backslash is part of the pattern. In a string passed to new RegExp(), write "\\r" so JavaScript produces the regex escape \r.
Replace CRLF and lone CR with LF: text.replace(/\r\n|\r/g, "\n"). Put \r\n before \r so a Windows pair becomes one newline, not two.
The \r escape is core regex syntax since ES3. It works in every browser and Node.js version.