The \r metacharacter matches a carriage return (CR) control character. It is a building block of line endings—especially Windows \r\n (CRLF)—and appears when parsing logs, CSV exports, and cross-platform text files.
01
CR char
\r
02
CRLF
\r\n
03
vs \n
LF alone
04
Normalize
→ \n
05
Split
Line breaks
06
ASCII 13
U+000D
Fundamentals
Introduction
Text files store invisible characters to mark where one line ends and the next begins. On Windows, that is usually \r\n (carriage return + line feed). On Linux and macOS today, it is typically \n alone.
When your JavaScript code reads files from different systems, regex patterns with \r help you detect, split, or clean up those line-ending differences.
Concept
Understanding the \r Metacharacter
\r matches exactly one carriage return character. It does not match a visible letter or digit—it matches a control code embedded in the string.
String.prototype.split("\n") alone misses CR-only or CRLF text—use a regex split for robustness.
Char code check: str.charCodeAt(i) === 13 identifies CR without regex.
Compatibility
Browser & Runtime Support
The \r carriage-return escape is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp \r
Supported in every browser and Node.js version. CRLF and normalization patterns behave consistently across runtimes.
99%Universal syntax
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
\rExcellent
Bottom line: Safe everywhere. Use \r when parsing cross-platform text with mixed line endings.
Wrap Up
Conclusion
The \r metacharacter matches carriage return control characters in strings. It is essential for handling Windows CRLF line endings, cleaning imported files, and splitting mixed-format text reliably.
Match \r\n for Windows breaks, normalize with /\r\n|\r/g, and always list CRLF before lone CR in alternations. Next, learn the \d digit metacharacter.
Confuse string "\\r" escaping with regex literal /\r/
Ignore trailing CR in CSV or form data
Display raw CR characters without normalizing for users
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp \r
Handle carriage return and CRLF line endings in JavaScript.
5
Core concepts
📄01
CR char
\r
Syntax
💻02
CRLF
\r\n
Windows
🔄03
Normalize
→ LF
Pattern
✂04
Split
Any EOL
Lines
⚠05
Order
CRLF first
Pitfall
❓ Frequently Asked Questions
\r matches the carriage return control character (CR, Unicode U+000D, ASCII 13). It moves the cursor to the start of the line in classic teletype systems and appears in some line-ending formats.
\r is carriage return (CR). \n is line feed (LF). Unix and Linux use \n alone for new lines. Windows traditionally uses both together as \r\n (CRLF).
Windows files often use CRLF (\r\n). Matching only \r can leave stray \n characters, and matching only \n can miss the \r. For full Windows line breaks, use /\r\n/g.
In a regex literal like /\r/, the backslash is part of the pattern. In a normal string passed to new RegExp(), write "\\r" so JavaScript produces the regex escape \r.
Replace all CRLF and lone CR with LF: text.replace(/\r\n|\r/g, "\n"). That converts Windows and old Mac endings to Unix-style newlines.
The \r escape is core regex syntax since ES3. It works in every browser and Node.js version.
Did you know?
The names “carriage return” and “line feed” come from typewriters and teletypes: CR moved the print head to the start of the line, and LF advanced the paper. Windows kept both characters in software line endings; Unix simplified to LF only.