The \n metacharacter matches a line feed (LF)—the newline character that separates lines in Unix-style text. It is essential for splitting paragraphs, counting lines, parsing logs, and working with multiline patterns.
01
LF char
\n
02
New line
Unix/macOS
03
Split
Lines
04
vs \r\n
Windows
05
m flag
Multiline
06
In \s
Whitespace
Fundamentals
Introduction
When you press Enter in a text area, JavaScript usually inserts a line feed character. That invisible code is what \n matches in a regular expression.
Newlines appear everywhere: multiline user input, server logs, CSV exports, and template strings. Regex with \n helps you split, count, validate, and transform line-based data.
Concept
Understanding the \n Metacharacter
\n matches exactly one line feed character. It is not the two-character sequence backslash-n in the source text—it is the single control code at that position in the string.
Line feed is part of the \s whitespace class. On Windows, a visible line break is often stored as \r\n, but many web APIs and modern files use \n alone.
💡
Beginner Tip
To split lines reliably across platforms, use /\r\n|\r|\n/ instead of only /\n/ when the source may be Windows or old Mac text.
Usage
How to Use \n in JavaScript
Common workflows include splitting, counting, replacing, and line-aware matching with the multiline flag:
Use split(/\n/) for line arrays, match(/\n/g) to count breaks, and the m flag when ^ and $ should apply per line.
Foundation
📝 Syntax
JavaScript
/\n/ // single line feed
/\n+/ // one or more newlines
/\r\n|\n/ // Windows or Unix line break
/^line$/m // whole line match (multiline)
/\s/ // whitespace (includes \n)
Common patterns
/\n/g — find every newline in text.
str.split(/\n/) — break into an array of lines.
/\r\n|\r|\n/g — split on any line-ending style.
/^# .+/gm — match comment lines in multiline text.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern / Code
Match newline
\n
String literal LF
"\n"
Split into lines
str.split(/\n/)
Count newlines
(str.match(/\n/g) || []).length
Any line ending
\r\n|\r|\n
Per-line anchors
/pattern/m
Compare
📋 \n vs \r vs \r\n vs \f
Four line-related control characters and endings.
Line feed
\n
Unix LF
Carriage return
\r
CR only
CRLF
\r\n
Windows
Form feed
\f
Page break
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover detection, splitting, counting, multiline anchors, and cross-platform line endings.
📚 Getting Started
Detect line feed characters in a string.
Example 1 — Test for a Newline Character
Return true when the text contains at least one line break.
String.prototype.split("\n") is fine when you know endings are LF-only.
Template literals use real newlines: `line1\nline2` contains \n characters.
Char code check: str.charCodeAt(i) === 10 identifies LF without regex.
Compatibility
Browser & Runtime Support
The \n line-feed escape is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp \n
Supported in every browser and Node.js version. Multiline (m) flag for per-line anchors is also universal.
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
\nExcellent
Bottom line: Safe everywhere. Use \n for line-based parsing and the m flag for per-line ^ and $.
Wrap Up
Conclusion
The \n metacharacter matches line feed newlines—the foundation of multiline text processing in JavaScript. Split lines, count breaks, and pair with the m flag for per-line pattern matching.
Remember cross-platform line endings and consider \r\n|\r|\n when files come from mixed sources. Next, learn the \D metacharacter for matching non-digits.
Split on \n only when CRLF pairs would leave stray \r
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp \n
Match and process newline characters in JavaScript.
5
Core concepts
📝01
Line feed
\n
Syntax
✂02
Split
Lines
Pattern
📈03
m flag
Per line
Anchors
💻04
CRLF
Windows
Compare
🔢05
In \s
Whitespace
Class
❓ Frequently Asked Questions
\n matches the line feed (LF) control character (Unicode U+000A, ASCII 10). It represents a new line in Unix, Linux, and modern macOS text.
In JavaScript source code, "\n" in a string literal creates a newline character. In regex, /\n/ matches that same LF character wherever it appears in text.
\n is a single line feed. Windows often stores \r\n (carriage return + line feed) as one line break. Match Windows endings with /\r\n/; match Unix-style lines with /\n/.
Yes. Line feed is included in the \s whitespace class along with space, tab, form feed, and carriage return.
The m flag makes ^ and $ match at line boundaries (before/after \n) instead of only at the start/end of the entire string.
The \n escape is core regex syntax since ES3. It works in every browser and Node.js version.
Did you know?
JavaScript template literals preserve real newline characters when you break lines inside backticks. That means a multiline template string already contains \n characters—no escape sequence required in the source.