Binary Number Regex
Validates that a string contains only the digits 0 and 1, i.e. a well-formed binary (base-2) number.
Regex Pattern
^[01]+$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| [01]+ | One or more characters, each of which must be the digit 0 or 1 |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern checks that every character in a string is either 0 or 1 and that there is at least one digit present. It rejects any string containing digits 2-9, letters, whitespace, or punctuation.
Why it works
The character class [01] restricts each position to exactly two allowed characters, and the + quantifier requires one or more of them. Anchoring with ^ and $ forces the entire string to consist of nothing but binary digits, so a mixed string like '101a' cannot partially match and slip through.
Common use cases
- Validating user-entered binary literals in a base converter tool
- Sanitizing input before parsing a string with parseInt(value, 2) or an equivalent
- Checking bitmask or flag strings in configuration files
- Teaching tools and coding exercises that validate binary representations
Edge cases
- A leading '0b' prefix (as in '0b1010') is not accepted by this pattern; strip the prefix first or extend the pattern if needed
- A string of all zeros like '0000' is valid since it still consists only of 0s and 1s
- An empty string is rejected because + requires at least one character
- Leading zeros are allowed since binary literals commonly retain them (e.g. '0010')
Limitations
- Does not enforce a maximum length, so extremely long strings of 0s and 1s will still match; add a bounded quantifier like {1,32} if a fixed bit width is required
- Does not accept common binary literal prefixes like '0b' or '0B' out of the box
- Does not validate the numeric value fits in a particular integer type (e.g. 8-bit, 32-bit) without an additional length check
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const binaryRegex = /^[01]+$/;
console.log(binaryRegex.test('1010')); // trueCommon Mistakes
Forgetting to anchor with ^ and $, so a string like '10a10' matches because '10' or '10' inside it is found unanchored
Fix: Always anchor the pattern or use a full-match method so the entire string must be binary digits
Assuming this regex also accepts a '0b' prefix used in programming language literals
Fix: Strip the prefix before validating, or extend the pattern to ^(0b)?[01]+$ if the prefix should be optional
Using [0-1] instead of [01] out of habit from wider ranges
Fix: For a two-character set like binary digits, [01] is clearer and equally correct; reserve range syntax for larger contiguous ranges
Performance Notes
- A single bounded character class with a + quantifier is one of the fastest regex shapes possible, with strictly linear-time matching
- No backtracking risk exists here since every character either matches [01] or the whole match fails immediately
- For extremely hot code paths validating fixed-width binary strings, a manual character loop can be marginally faster than regex, but the difference is negligible for typical input sizes
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |