JavaScript RegExp Carriage Return Metacharacter (\r)

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is \r?

\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
UsePrefer
Detect a CR/\r/
Windows line break/\r\n/
Normalize to LFreplace(/\r\n|\r/g, "\n")
Split any endingsplit(/\r\n|\r|\n/)

How to Use \r

Treat carriage return as a concrete character — test for it, match CRLF as a pair, or normalize mixed endings.

  1. Create or import text that may contain CR (string escape: "\r").
  2. Use /\r/ with test, match, split, or replace.
  3. For Windows files, match /\r\n/ as one unit when you care about the full ending.
  4. When normalizing, list \r\n before \r so CRLF does not become two replacements.
PieceRequired?Role
\r in the patternYesMatch the carriage return character
String escape "\r"For demos / fixturesInsert CR into a JavaScript string
g flagOptionalFind every CR or CRLF
Alternation orderWhen normalizingPut \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

Cheat Sheet

Keep these facts handy when working with carriage returns.

TopicDetail
CharacterCarriage return — U+000D (ASCII 13)
Regex / string/\r/ · "\r" · new RegExp("\\r")
vs LF / CRLF\r ≠ \n; CRLF is \r\n
WhitespaceIncluded in \s
Normalizereplace(/\r\n|\r/g, "\n")
Mixed split/\r\n|\r|\n/ (list CRLF first)
StandardES3+ (universal)

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 */

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.

JavaScript
console.log(/\r/.test("hello\rworld")); // true
console.log(/\r/.test("hello\nworld")); // false
console.log(/\r/.test("a\r\nb"));       // true (CRLF includes CR)
Try It Yourself

How It Works

  1. CR present. "hello\rworld" contains U+000D → true.
  2. LF only. "hello\nworld" has line feed, not carriage return → false.
  3. Windows CRLF. "a\r\nb" starts the break with CR, so /\r/ matches.
  4. Takeaway. \r and \n are different characters — never treat them as interchangeable.

Example 2 — Match Windows CRLF Pairs

Find every \r\n sequence as one line break.

JavaScript
const windowsText = "row1\r\nrow2\r\nrow3";

const breaks = windowsText.match(/\r\n/g);
console.log(breaks);                 // ["\r\n", "\r\n"]
console.log(breaks.length);          // 2
console.log(/\r\n/.test(windowsText)); // true
Try It Yourself

How It Works

  1. Pattern is two characters. /\r\n/ needs CR immediately followed by LF.
  2. Global match. The g flag collects every CRLF pair in the string.
  3. Count breaks. Two pairs mean three rows of data.
  4. Unit match. Matching the pair avoids treating CR and LF as separate cuts.

Example 3 — Normalize Line Endings to LF

Convert CRLF and lone CR to Unix-style newlines, then split cleanly.

JavaScript
const mixed = "line1\r\nline2\rline3\nline4";

const unix = mixed.replace(/\r\n|\r/g, "\n");
console.log(JSON.stringify(unix));
// "line1\nline2\nline3\nline4"

console.log(unix.split("\n"));
// ["line1", "line2", "line3", "line4"]
Try It Yourself

How It Works

  1. List CRLF first. \r\n before \r turns each Windows pair into one \n.
  2. Lone CR next. Old Mac-style breaks become LF too.
  3. Existing LF stays. Unix newlines are already correct and are left alone.
  4. Split simply. After normalizing, split("\n") is enough.

Edge Cases & Pitfalls

Check these before relying on \r in real projects.

order

Put \r\n before \r

If you replace \r first, a CRLF pair becomes two replacements. Use /\r\n|\r/g.

trap

split(/\n/) on CRLF leaves \r

"a\r\nb".split(/\n/) yields ["a\r", "b"]. Prefer /\r\n|\r|\n/ for mixed files.

escape

new RegExp needs double escaping

Write new RegExp("\\r"). In a string pattern argument you need two backslashes.

vs

\r is not \n

Unix text often has no CR at all. Detect LF with /\n/ when that is what you mean.

Browser & Runtime Support

The \r carriage-return escape is core regex syntax (ES3+) and works everywhere JavaScript runs.

✓ Baseline · ES3+

RegExp \r

Supported in every modern and legacy browser, plus all Node.js versions. No polyfill required.

100% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge Supported · Chromium & Legacy
Full support
Internet Explorer Supported · Legacy
Full support
Opera All modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
\r Excellent

Bottom line: Safe to use in any JavaScript environment. Focus on line-ending style (CR vs CRLF vs LF) rather than browser support.

Performance Notes

ApproachCostBest for
/\r/.test(str)O(n)Quick CR presence checks
str.match(/\r\n/g)O(n)Count Windows line breaks
str.replace(/\r\n|\r/g, "\n")O(n)Normalize before further parsing

Matching a single control character is cheap. Prefer a one-pass normalize when files may mix CRLF and CR, then use simple \n logic afterward.

Key Takeaways

  • Role: \r matches the carriage return character (U+000D).
  • CRLF: Windows endings are \r\n — match the pair when you need the full break.
  • Normalize: replace(/\r\n|\r/g, "\n") — list CRLF first.
  • Not LF: use \n when you mean line feed.

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.

Next: \d digit

Learn how the \d metacharacter matches any digit from 0 to 9.

\d tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful