JavaScript RegExp Null Character Metacharacter (\0)

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

What Is \0?

\0 matches the null character (NUL, U+0000, code point 0). It is invisible in normal text and often used as a separator or terminator in legacy / binary-oriented data.

Remember
\0     → null character (U+0000)
\x00   → same null (hex form — safer near digits)
\u0000 → same null (Unicode form in strings/patterns)

/\0/.test("hello")         → false
/\0/.test("hello\u0000")   → true
"a\u0000b".replace(/\x00/g, "") → "ab"
UsePrefer
Detect a null byte/\0/ or /\x00/
Remove null bytesstr.replace(/\x00/g, "")
Split on NULstr.split(/\0/)
Hex escapes\xdd

How to Use \0

Treat NUL as a real character you can test for, split on, or strip out.

  1. Create fixtures with "\u0000" or String.fromCharCode(0).
  2. Use /\0/ or /\x00/ with test, split, or replace.
  3. Add the g flag when every null byte must be removed.
  4. Prefer \x00 if the next pattern character might be a digit.
PieceRequired?Role
\0 / \x00 in the patternYesMatch the null character
String escapeFor demos"\u0000" inserts NUL into a string
g flagOptionalFind or replace every null byte
Not whitespace—\s does not include NUL

Pseudocode

Pseudocode
pattern has "\0" (or "\x00")
read next character ch from the string

if ch.codePoint === 0:   // NUL
    accept match
else:
    fail this path

common actions:
  test(str)              → true if any NUL exists
  split(str)             → parts between NULs
  replace(/\x00/g, "")   → strip every NUL

Cheat Sheet

Keep these facts handy when sanitizing null bytes.

TopicDetail
CharacterNUL — U+0000 (code point 0)
Regex forms\0 · \x00 · \u0000
Safer near digits\x00 (avoid octal ambiguity)
Whitespace?No — not part of \s
Detect/\0/.test(str)
Stripstr.replace(/\x00/g, "")
StandardES3+ (universal)

Live Preview

Run common \0 operations on sample strings that contain a null byte.

Browser demo of the RegExp \0 null escape — same ideas as the examples below.

Result
text = "hello"
/\0/.test(text)
→ false

/* no null character present */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Detect 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

  1. Plain text. "hello" has no code point 0 → false.
  2. Insert NUL. "\u0000" embeds a real null character in the string.
  3. Pattern matches. /\0/ finds that character anywhere in the string.
  4. Invisible but real. You may not see NUL in the console, but test still returns true.

Example 2 — 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

  1. Start dirty. Two NUL bytes sit inside "user…name…".
  2. Use hex form. /\x00/g matches every null without octal ambiguity.
  3. Replace with empty. Each NUL is deleted; letters remain.
  4. Check length. "username" is 8 characters — the nulls are gone.

Example 3 — Compare Escape Forms

Verify that \0 and \x00 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
Try It Yourself

How It Works

  1. One character. Index 1 in "x…y" is the null byte.
  2. Both patterns match. \0 and \x00 refer to the same code point.
  3. Confirm with code. charCodeAt(1) returns 0.
  4. Debugging tip. When NUL is hard to see, print charCodeAt or JSON.stringify.

Edge Cases & Pitfalls

Check these before relying on \0 in real projects.

octal

\0 + digit can become octal

Prefer \x00 when the next character in the pattern is a digit. Legacy octal forms are easy to misread.

invisible

NUL does not show in normal output

Use charCodeAt, JSON.stringify, or replace NUL with a visible marker while debugging.

whitespace

Not part of \s

Whitespace cleanup with /\s/g will not remove null bytes. Strip them with /\x00/g separately.

escape

new RegExp needs careful escaping

Prefer new RegExp("\\x00") or a regex literal /\x00/. Double-check backslashes in string patterns.

Browser & Runtime Support

The \0 / \x00 null escapes are core regex syntax (ES3+) and work everywhere JavaScript runs.

✓ Baseline · ES3+

RegExp \0

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

Bottom line: Safe to use in any JavaScript environment. Prefer \x00 in patterns that sit next to digits.

Performance Notes

ApproachCostBest for
/\0/.test(str)O(n)Quick presence checks
str.replace(/\x00/g, "")O(n)Sanitizing input
str.split(/\0/)O(n)NUL-separated segments

Matching a single control character is cheap. Prefer an explicit \x00 strip when cleaning untrusted text; do not assume \s will remove NUL.

Key Takeaways

  • Role: \0 matches the null character U+0000.
  • Same as: \x00 / \u0000 — prefer hex near digits.
  • Sanitize: replace(/\x00/g, "") removes every null byte.
  • Not whitespace: \s will not strip NUL for you.

One line: \0 finds the null byte — detect it, strip it with \x00, and do not confuse it with whitespace.

Frequently Asked Questions

\0 matches the null character (NUL, Unicode U+0000, code point 0). It is an invisible control character with no printable glyph.
Yes. Both \0 and \x00 match the single null byte. Prefer \x00 (or \u0000) when the next character might be a digit, because \0 followed by digits 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.
Use "\0", "\x00", "\u0000", or String.fromCharCode(0). In examples, \u0000 is easy to read in source code.
The \0 null escape is core regex syntax since ES3. It works in every browser and Node.js version.

Next: \s whitespace

Learn how the whitespace metacharacter matches spaces, tabs, and newlines.

\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