\n matches the line feed (LF) character (U+000A, ASCII 10) — the usual newline on Unix, Linux, and modern macOS. Use it to detect, split, or normalize line breaks in text.
Remember
\n → line feed / newline (U+000A)
\r → carriage return (different character)
\r\n → Windows-style line ending (CR + LF)
\s → any whitespace, including \n
"a\nb" → two lines in a string
/\n/.test(...) → true when LF is present
split(/\n/) → break into lines
Treat line feed as a concrete character — test for it, split on it, or normalize mixed endings.
Create or import text with newlines (string escape: "\n").
Use /\n/ with test, match, split, or replace.
Add the g flag when you need every newline, not just the first.
For Windows/legacy files, prefer /\r\n|\r|\n/ so CRLF is not split twice.
Piece
Required?
Role
\n in the pattern
Yes
Match the line feed character
String escape "\n"
For demos / fixtures
Insert LF into a JavaScript string
g flag
Optional
Find every newline
m flag
Optional
Make ^ / $ work per line
Pseudocode
Pseudocode
pattern has "\n"
read next character ch from the string
if ch.codePoint === 0x000A: // line feed
accept match
else:
fail this path
common actions:
test(str) → true/false
split(str) → one entry per line
replace(/\r\n|\r|\n/g) → normalize endings
Reference
Cheat Sheet
Keep these facts handy when working with line breaks.
Topic
Detail
Character
Line feed — U+000A (ASCII 10)
Regex / string
/\n/ · "\n" · new RegExp("\\n")
vs CR / CRLF
\n ≠ \r; CRLF is \r\n
Whitespace
Included in \s
Split lines
text.split(/\n/)
Mixed endings
/\r\n|\r|\n/ (list CRLF first)
Standard
ES3+ (universal)
Try it
Live Preview
Run common \n operations on sample multiline text.
Browser demo of the RegExp \n line feed escape — same ideas as the examples below.
Result
text = "two\nlines"
/\n/.test(text)
→ true
/* line feed (U+000A) 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 Newline
Return true when the text contains at least one line feed.
One line:\n finds line feeds — split LF text with /\n/, and use /\r\n|\r|\n/ when endings may differ.
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/; a lone /\n/ still matches the LF half of CRLF.
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 newlines) instead of only at the start/end of the entire string. See the modifier m tutorial for details.
The \n escape is core regex syntax since ES3. It works in every browser and Node.js version.