JavaScript RegExp Null Character Metacharacter (\0)
Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview
Definition
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"
Treat NUL as a real character you can test for, split on, or strip out.
Create fixtures with "\u0000" or String.fromCharCode(0).
Use /\0/ or /\x00/ with test, split, or replace.
Add the g flag when every null byte must be removed.
Prefer \x00 if the next pattern character might be a digit.
Piece
Required?
Role
\0 / \x00 in the pattern
Yes
Match the null character
String escape
For demos
"\u0000" inserts NUL into a string
g flag
Optional
Find 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
Reference
Cheat Sheet
Keep these facts handy when sanitizing null bytes.
Topic
Detail
Character
NUL — 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)
Strip
str.replace(/\x00/g, "")
Standard
ES3+ (universal)
Try it
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 */
Code
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.
Both patterns match.\0 and \x00 refer to the same code point.
Confirm with code.charCodeAt(1) returns 0.
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.
Compatibility
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 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 to use in any JavaScript environment. Prefer \x00 in patterns that sit next to digits.
Analysis
Performance Notes
Approach
Cost
Best 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.
Remember
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.