JavaScript RegExp Newline Metacharacter (\n)

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

What Is \n?

\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
UsePrefer
Detect a newline/\n/
Split into linesstr.split(/\n/)
Mixed endings/\r\n|\r|\n/
Per-line ^ / $/m flag

How to Use \n

Treat line feed as a concrete character — test for it, split on it, or normalize mixed endings.

  1. Create or import text with newlines (string escape: "\n").
  2. Use /\n/ with test, match, split, or replace.
  3. Add the g flag when you need every newline, not just the first.
  4. For Windows/legacy files, prefer /\r\n|\r|\n/ so CRLF is not split twice.
PieceRequired?Role
\n in the patternYesMatch the line feed character
String escape "\n"For demos / fixturesInsert LF into a JavaScript string
g flagOptionalFind every newline
m flagOptionalMake ^ / $ 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

Cheat Sheet

Keep these facts handy when working with line breaks.

TopicDetail
CharacterLine feed — U+000A (ASCII 10)
Regex / string/\n/ · "\n" · new RegExp("\\n")
vs CR / CRLF\n ≠ \r; CRLF is \r\n
WhitespaceIncluded in \s
Split linestext.split(/\n/)
Mixed endings/\r\n|\r|\n/ (list CRLF first)
StandardES3+ (universal)

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

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.

JavaScript
console.log(/\n/.test("one line"));    // false
console.log(/\n/.test("two\nlines"));  // true
console.log(/\n/.test("a\r\nb"));      // true (CRLF includes LF)
Try It Yourself

How It Works

  1. Single line. "one line" has no LF, so /\n/ returns false.
  2. Unix newline. "two\nlines" contains U+000A → true.
  3. Windows CRLF. "a\r\nb" is CR + LF; /\n/ still matches the LF half.
  4. Takeaway. Detecting LF is not the same as normalizing every line-ending style.

Example 2 — Split Text into Lines

Break a multiline string into an array of rows.

JavaScript
const log = "INFO boot\nWARN slow\nERROR fail";

const lines = log.split(/\n/);
console.log(lines);
// ["INFO boot", "WARN slow", "ERROR fail"]

console.log(lines[1]); // "WARN slow"
Try It Yourself

How It Works

  1. Start with LF separators. Two newlines create three log lines.
  2. Split on \n. Each match is a cut point; the LF itself is removed.
  3. Index a row. lines[1] is the second line: "WARN slow".
  4. Unix-only note. Pure split(/\n/) is ideal when the file already uses LF.

Example 3 — Split Mixed Line Endings

Handle Unix (\n), Windows (\r\n), and old Mac (\r) breaks safely.

JavaScript
const mixed = "a\r\nb\nc\rd";

const lines = mixed.split(/\r\n|\r|\n/);
console.log(lines); // ["a", "b", "c", "d"]

const normalized = mixed.replace(/\r\n|\r/g, "\n");
console.log(JSON.stringify(normalized));
// "a\nb\nc\nd"
Try It Yourself

How It Works

  1. List CRLF first. \r\n before \r or \n stops Windows endings from becoming two cuts.
  2. Split once. Each style of line break produces clean parts: a, b, c, d.
  3. Normalize. Replace CR and CRLF with \n so the rest of your code can assume Unix newlines.
  4. Inspect escapes. JSON.stringify shows \n clearly in the console.

Edge Cases & Pitfalls

Check these before relying on \n in real projects.

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("\\n"). In a string pattern argument you need two backslashes.

dot

Default . does not match \n

Without the s (dotAll) flag, the wildcard dot stops at newlines. See the dot tutorial.

anchors

^ / $ need the m flag per line

Without m, anchors apply only to the whole string. Use /m for line-start matching.

Browser & Runtime Support

The \n line feed escape is core regex syntax (ES3+) and works everywhere JavaScript runs.

✓ Baseline · ES3+

RegExp \n

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

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · 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
\n Excellent

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

Performance Notes

ApproachCostBest for
/\n/.test(str)O(n)Quick presence checks
str.split(/\n/)O(n)Unix / LF-only text
str.split(/\r\n|\r|\n/)O(n)Mixed or unknown line endings

Matching a single control character is cheap. Prefer the mixed alternation only when files may use CRLF or bare \r; otherwise /\n/ is enough.

Key Takeaways

  • Role: \n matches the line feed newline character (U+000A).
  • CRLF: Windows \r\n still contains \n, but split carefully.
  • Common jobs: test, split, and normalize mixed endings.
  • Anchors: use the m flag so ^ / $ apply per line.

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.

Next: \D non-digit

Learn how the \D metacharacter matches any character that is not a digit.

\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