JavaScript RegExp \f Metacharacter

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Form feed

What You’ll Learn

The \f metacharacter matches a form feed (FF) control character—an invisible code historically used for page breaks on printers. Modern web apps rarely create form feeds, but regex helps you detect and clean them in legacy files and exports.

01

FF char

\f

02

ASCII 12

U+000C

03

Whitespace

In \s

04

vs \n

Different

05

Split

Sections

06

Clean

replace

Introduction

Control characters are invisible codes embedded in strings. You already know \n for new lines and \r for carriage return. Form feed (\f) is another—it once told printers to start a new page.

You will not see form feeds in everyday user input, but they can appear in old documents, mainframe exports, and some PDF-to-text conversions. Regex lets you find and normalize them.

Understanding the \f Metacharacter

\f matches exactly one form feed character. Like other control codes, it is not visible when rendered but occupies one position in the string.

JavaScript
const text = "page1\fpage2";

/\f/.test(text);        // true
/\f/.test("hello");     // false

text.split("\f");       // ["page1", "page2"]

Form feed is also matched by the whitespace shorthand \s, which covers space, tab, newline, carriage return, form feed, and related separators.

💡
Beginner Tip

To debug invisible characters, use JSON.stringify(str). Form feed appears as "\\f" in the output, making it easy to spot in DevTools.

How to Use \f in JavaScript

Typical tasks include detecting form feeds, splitting sections, and replacing them with newlines for display:

JavaScript
const raw = "Intro\fChapter 1\fChapter 2";

raw.match(/\f/g);              // ["\f", "\f"]
raw.split(/\f/);               // ["Intro", "Chapter 1", "Chapter 2"]
raw.replace(/\f/g, "\n\n");    // readable paragraphs

Use test() to check for presence, match() to count breaks, and replace() to convert form feeds into display-friendly spacing.

📝 Syntax

JavaScript
/\f/              // single form feed
/\f+/             // one or more form feeds
/\f/g             // all form feeds (global)
/\s/              // any whitespace (includes \f)
/[\f\n\r]+/       // form feed or line breaks

Common patterns

  • /\f/g — find every form feed in a document.
  • str.split(/\f/) — split into page-like sections.
  • str.replace(/\f/g, "\n") — normalize to newlines.
  • /[^\f]+/g — match text runs between form feeds.

⚡ Quick Reference

GoalPattern / Code
Match form feed\f
String literal FF"\f"
Split on page breaksstr.split(/\f/)
Replace with newlinestr.replace(/\f/g, "\n")
Any whitespace\s (includes \f)
Char code checkcharCodeAt(i) === 12

📋 \f vs \n vs \r vs \s

Four related whitespace and control-character patterns.

Form feed
\f

Page break

Line feed
\n

New line

Carriage return
\r

CR char

Whitespace
\s

All incl. \f

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover detection, splitting, replacement, whitespace classes, and cleanup.

📚 Getting Started

Detect form feed characters in a string.

Example 1 — Test for Form Feed

Check whether text contains a form feed control character.

JavaScript
const withFF = "section\fnext";
const plain = "section next";

console.log(/\f/.test(withFF));  // true
console.log(/\f/.test(plain));   // false
console.log(/\n/.test(withFF));  // false
Try It Yourself

How It Works

\f, \n, and \r are distinct characters. A form feed in the string matches only /\f/, not newline patterns.

📈 Practical Patterns

Split, replace, and clean form feed separators.

Example 2 — Split Text into Sections

Break a legacy document on form feed page markers.

JavaScript
const doc = "Title\fChapter 1\fChapter 2\fIndex";

const sections = doc.split(/\f/);
console.log(sections);
// ["Title", "Chapter 1", "Chapter 2", "Index"]

console.log(sections.length); // 4
Try It Yourself

How It Works

Each \f acts as a delimiter. Splitting produces an array of section strings without the control characters.

Example 3 — Replace Form Feeds with Newlines

Convert invisible page breaks into readable line spacing.

JavaScript
const legacy = "Page A\fPage B\fPage C";

const readable = legacy.replace(/\f/g, "\n\n");
console.log(JSON.stringify(readable));
// "Page A\n\nPage B\n\nPage C"
Try It Yourself

How It Works

Global replace swaps every form feed for a double newline, making legacy page breaks visible in modern text displays.

Example 4 — Form Feed Inside \s Whitespace

See how \s matches form feed along with spaces and tabs.

JavaScript
const mixed = "a\f b\tc";

console.log(/\s/.test(mixed));           // true
console.log(mixed.match(/\s/g));        // ["\f", " ", "\t"]

console.log(/\f/.test(mixed));          // true
console.log(mixed.replace(/\s/g, "-")); // "a-b-c"
Try It Yourself

How It Works

Form feed counts as whitespace in JavaScript. Use /\f/ when you need that specific character, or /\s/ for any whitespace type.

Example 5 — Strip All Form Feeds from Imported Text

Remove page-break characters before storing or searching content.

JavaScript
const imported = "Report\fSummary\f2026";

const cleaned = imported.replace(/\f/g, " ");
console.log(cleaned);              // "Report Summary 2026"
console.log(/\f/.test(cleaned));   // false
Try It Yourself

How It Works

Replacing form feeds with spaces (or empty strings) produces clean, searchable text without invisible control codes.

🚀 Common Use Cases

  • Legacy file import — detect form feeds in old text exports.
  • Document normalization — convert page breaks to newlines for HTML display.
  • Section parsing — split reports into chapters or pages.
  • Search indexing — strip control chars before tokenizing.
  • Data validation — flag unexpected control characters in uploads.
  • Whitespace cleanup — combine with \s for full whitespace normalization.

Important Considerations

  • Invisible character — form feed does not show in normal UI; use JSON.stringify to debug.
  • Not a newline — browsers may not render \f as a visible line break without replacement.
  • Included in \s — broad whitespace patterns will match form feed even if you only wanted spaces.
  • Rare in modern data — most web text uses \n; expect \f mainly in legacy sources.
  • String escaping — in new RegExp() strings, write "\\f" for the regex escape.

🧠 How \f Matches Text

1

Scan string

The engine reads each character, including invisible control codes.

Input
2

Compare to \f

Only ASCII 12 (U+000C) satisfies a form feed pattern.

FF
3

Act on match

Split, replace, or validate depending on your processing goal.

Process
4

Clean output

Normalized text without stray page-break codes.

📝 Notes

  • \f is form feed; \n is line feed; they serve different purposes.
  • Form feed is part of the \s whitespace set in JavaScript regex.
  • Review \r carriage return and upcoming \n newline.
  • Char code 12 identifies form feed: "\f".charCodeAt(0) === 12.
  • For general whitespace cleanup, /\s+/g also removes form feeds.
  • Modern apps rarely emit \f; treat it as a legacy-format signal.

Browser & Runtime Support

The \f form-feed escape is part of core JavaScript regular expression syntax.

Baseline · ES3+

RegExp \f

Supported in every browser and Node.js version. Form feed matching behaves consistently across all JavaScript runtimes.

99% Universal syntax
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
\f Excellent

Bottom line: Safe everywhere. Use \f when cleaning or parsing legacy text with page-break characters.

Conclusion

The \f metacharacter matches form feed control characters—legacy page-break markers that still appear in some imported documents. Detect them with /\f/, split sections with split, and normalize with replace.

Remember that form feed is whitespace in \s but distinct from \n and \r. Next, learn the \n newline metacharacter.

💡 Best Practices

✅ Do

  • Use JSON.stringify to debug invisible FF chars
  • Replace \f with \n for display
  • Split on /\f/ for legacy page sections
  • Use /\f/g when removing all form feeds
  • Know that \s also matches form feed

❌ Don’t

  • Confuse \f with \n or \r
  • Expect form feeds to render as visible line breaks in HTML
  • Assume modern user input contains form feeds
  • Forget the global flag when replacing all occurrences
  • Mix up string "\\f" and regex /\f/ escaping

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp \f

Handle form feed page-break characters in JavaScript.

5
Core concepts
🔢 02

ASCII 12

U+000C

Code
03

Split

Sections

Pattern
🛠 04

In \s

Whitespace

Compare
🔄 05

Replace

→ \n

Clean

❓ Frequently Asked Questions

\f matches the form feed control character (FF, Unicode U+000C, ASCII 12). It was originally used to advance to the next page on printers and still appears in some legacy text files.
No. \f is form feed (page break). \n is line feed (new line). They are different control characters, though both can act as whitespace separators in text processing.
Yes. Form feed is included in the \s whitespace class along with space, tab, newline, carriage return, and other Unicode space separators.
Use an escape in a string literal: "page1\fpage2". In regex literals write /\f/; in new RegExp() strings use "\\f".
Mostly when parsing or cleaning legacy documents, printer-formatted data, or exported files that embed form feed characters as page or section breaks.
The \f escape is core regex syntax since ES3. It works in every browser and Node.js version.
Did you know?

In the ASCII control-character table, form feed (12) sits between vertical tab (11) and carriage return (13). Together with line feed (10), these codes shaped early text file formats long before the web existed.

Continue to \n newline metacharacter

Learn how the line-feed character matches new lines in strings.

\n 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.

6 people found this page helpful