JavaScript RegExp \0 Metacharacter

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Null (NUL) U+0000

What You’ll Learn

The \0 metacharacter matches the null character (NUL, Unicode U+0000). It is invisible, has ASCII code point 0, and appears in legacy binary data and C-style null-terminated strings. You will learn to detect, split, and remove null bytes safely.

01

Null byte

\0

02

Unicode

U+0000

03

Hex form

\x00

04

Detect

/\0/.test()

05

Remove

/\0/g

06

Split

split(/\0/)

Introduction

Most web text never contains a null character. But in systems programming, file parsing, and security hardening, the NUL byte still matters. The \0 escape lets your regex target that invisible code point directly.

In JavaScript strings, you can create a null character with "\0", "\u0000", or "\x00". In a regular expression, write /\0/ or the safer /\x00/ to match it.

Understanding the \0 Metacharacter

The null character marks the end of strings in C and appears in some binary formats. In JavaScript, strings can contain embedded nulls, and \0 in a regex matches exactly one such character.

JavaScript
/\0/.test("hello");           // false
/\0/.test("hello\u0000");    // true

"a\u0000b\u0000c".split(/\0/);     // ["a", "b", "c"]
"safe\u0000data".replace(/\0/g, ""); // "safedata"

Because null is invisible, use charCodeAt() or includes("\0") alongside regex when debugging.

💡
Beginner Tip

Prefer /\x00/g over /\0/g when writing regex literals. If \0 is followed by a digit (like \01), JavaScript may interpret it as an octal escape instead of null plus a digit.

How to Use \0 in JavaScript

Use \0 in detection, splitting, sanitization, and validation workflows:

JavaScript
/\0/.test(userInput);              // detect null bytes
text.split(/\0/);                  // split C-style segments
text.replace(/\x00/g, "");         // strip all nulls (safe form)
!/\0/.test(filename);              // reject filenames with NUL

Always use the global flag when removing every null byte with replace().

📝 Syntax

JavaScript
\0              // null character (NUL)
\x00            // same null byte (hex, safer in regex)
\u0000          // null in string literals
/\0/g           // global: every null byte
/\0+/           // one or more consecutive nulls

Common patterns

  • /\0/ — detect whether any null byte exists.
  • /\x00/g — remove all null characters from input.
  • split(/\0/) — break a string at null terminators.
  • /^(?!.*\0).*$/ — reject strings containing null.

⚡ Quick Reference

GoalPattern
One null byte\0 or \x00
Detect null/\0/.test(str)
Remove all nulls/\x00/g + replace("", ...)
Split at nullsplit(/\0/)
String literal"\u0000"
Not whitespace\0\s

📋 \0 vs \x00 vs \u0000

Three ways to represent the same null character.

Octal-style
\0

Regex escape

Hex
\x00

Safer in regex

Unicode
\u0000

String literal

Code point
U+0000

NUL name

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover detection, splitting, removal, validation, and escape-form comparison.

📚 Getting Started

Detect whether a string contains a null byte.

Example 1 — Test for a Null Character

Return true when an invisible NUL byte appears anywhere in the text.

JavaScript
console.log(/\0/.test("hello"));        // false
console.log(/\0/.test("hello\u0000"));     // true
console.log(/\0/.test("a\u0000b"));       // true
Try It Yourself

How It Works

Normal text has no null bytes. The pattern succeeds only when \u0000 (or equivalent) is embedded in the string.

📈 Practical Patterns

Split, sanitize, validate, and compare null-byte patterns.

Example 2 — Split a C-Style Null-Terminated String

Break text into segments at each null byte separator.

JavaScript
const raw = "apple\u0000banana\u0000cherry";

const parts = raw.split(/\0/);
console.log(parts); // ["apple", "banana", "cherry"]

console.log(parts.length); // 3
Try It Yourself

How It Works

split(/\0/) treats each NUL as a delimiter, similar to how C strings use null as a terminator between concatenated values.

Example 3 — Remove All Null Bytes

Strip invisible null characters from untrusted input before storage.

JavaScript
const dirty = "user\u0000name\u0000";

const clean = dirty.replace(/\x00/g, "");
console.log(clean);              // "username"
console.log(clean.length);       // 8
Try It Yourself

How It Works

The global flag with /\x00/g removes every null byte. Using hex form avoids octal ambiguity with trailing digits.

Example 4 — Reject Input Containing Null Bytes

Validate that a filename or username has no embedded NUL characters.

JavaScript
function isNullFree(value) {
  return !/\0/.test(value);
}

console.log(isNullFree("report.pdf"));       // true
console.log(isNullFree("doc\u0000.pdf"));     // false
console.log(isNullFree("hello"));            // true
Try It Yourself

How It Works

Negating /\0/.test() gives a simple null-free check. Many systems reject NUL in paths and identifiers to prevent truncation bugs.

Example 5 — Compare Escape Forms

Verify that \0, \x00, and \u0000 all match the same character.

JavaScript
const sample = "x\u0000y";

console.log(/\0/.test(sample));    // true
console.log(/\x00/.test(sample));  // true

console.log(sample.charCodeAt(1)); // 0 (NUL code point)
Try It Yourself

How It Works

All three escapes refer to Unicode code point 0. Use charCodeAt() to confirm the invisible character when debugging.

🚀 Common Use Cases

  • Input sanitization — strip null bytes from user-provided strings before saving.
  • Filename validation — reject paths containing NUL to avoid truncation issues.
  • Binary parsing — split legacy null-delimited records with split(/\0/).
  • Security checks — detect null-byte injection in uploaded content.
  • Data migration — clean imported files that embed unexpected NUL characters.
  • Debugging — locate invisible characters that break string comparisons.

Important Considerations

  • Invisible character — null prints nothing; use charCodeAt() or length checks to debug.
  • Octal ambiguity\0 followed by digits may parse as octal; prefer \x00 in regex.
  • Not whitespace\0 is not matched by \s.
  • Global flag — omitting g in replace() removes only the first null.
  • Rare in web text — most UI strings never contain NUL; handle it mainly for security and binary edge cases.

🧠 How \0 Matches the Null Character

1

Read character

The engine inspects the next character at the current position.

Scan
2

Check code point 0

\0 succeeds only if the character is U+0000 (NUL).

Match
3

Apply global flag

With g, the engine finds every null byte in the string.

Extend
4

Continue pattern

Null matched → advance. Printable char → fail or backtrack.

📝 Notes

  • \0, \x00, and \u0000 all represent the same null character.
  • Null is U+0000 with numeric value 0 in charCodeAt().
  • Review \n and other control-character metacharacters.
  • Next up: \s whitespace metacharacter.
  • Prefer /\x00/g over /\0/g when digits may follow in the pattern.
  • Null bytes are invalid in many file systems and protocols—validate early.

Browser & Runtime Support

The \0 null metacharacter is part of core JavaScript regular expression syntax.

Baseline · ES3+

RegExp \0

Supported in every browser and Node.js version. \0 and \x00 both match the NUL character.

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
\0 Excellent

Bottom line: Safe everywhere. Use \x00 in regex when you want to avoid octal ambiguity.

Conclusion

The \0 metacharacter matches the null character (NUL, U+0000)—an invisible byte with code point zero. While rare in everyday web text, it matters for sanitization, binary parsing, and security validation.

Use /\0/.test() to detect null bytes, replace(/\x00/g, "") to remove them, and split(/\0/) for null-delimited data. Next, learn \s for whitespace matching.

💡 Best Practices

✅ Do

  • Use /\x00/g for safe null-byte removal
  • Validate filenames and paths with !/\0/.test()
  • Use charCodeAt() when debugging invisible chars
  • Strip nulls from untrusted input before storage
  • Know that \0 is not whitespace

❌ Don’t

  • Assume users never submit null bytes
  • Forget the global flag when removing all nulls
  • Use \01 expecting null plus "1" (octal risk)
  • Confuse \0 with the digit 0
  • Display null-containing strings without sanitizing first

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp \0

Match the invisible NUL character at code point zero.

5
Core concepts
📝 02

U+0000

Code point.

Unicode
📈 03

\x00

Hex form.

Safer
🔒 04

/\x00/g

Strip nulls.

Sanitize
🔢 05

split(/\0/)

C-strings.

Parse

❓ Frequently Asked Questions

\0 matches the null character (NUL, Unicode U+0000, ASCII code point 0). It is an invisible control character with no printable glyph.
Yes. Both \0 and \x00 match the single null byte. \x00 is often preferred in regex because \0 followed by another digit can be read as an octal escape.
Null bytes appear in legacy binary data, C-style null-terminated strings, and occasionally in malformed or malicious input. Regex helps detect, split, or remove them during sanitization.
No. The \s whitespace class includes space, tab, newline, and related separators—but not the null character.
Rarely on purpose, but null bytes can appear in copied binary data or crafted payloads. Validating with /\0/ and stripping with replace(/\0/g, "") is a simple defense.
The \0 null escape is core regex syntax since ES3. It works in every browser and Node.js version.
Did you know?

In C, strings end at the first \0 byte. JavaScript strings have an explicit .length and can contain embedded nulls anywhere—which is why detecting NUL with regex matters when processing imported binary or legacy data.

Continue to \s whitespace metacharacter

Learn how lowercase s matches spaces, tabs, and line breaks.

\s 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