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/)
Fundamentals
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.
Concept
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.
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.
Usage
How to Use \0 in JavaScript
Use \0 in detection, splitting, sanitization, and validation workflows:
Always use the global flag when removing every null byte with replace().
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern
One null byte
\0 or \x00
Detect null
/\0/.test(str)
Remove all nulls
/\x00/g + replace("", ...)
Split at null
split(/\0/)
String literal
"\u0000"
Not whitespace
\0 ≠ \s
Compare
📋 \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
Hands-On
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.
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.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
\0Excellent
Bottom line: Safe everywhere. Use \x00 in regex when you want to avoid octal ambiguity.
Wrap Up
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.
Display null-containing strings without sanitizing first
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp \0
Match the invisible NUL character at code point zero.
5
Core concepts
🚫01
NUL byte
\0
Syntax
📝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.