Octal Number Regex
Validates a base-8 (octal) number, accepting digits 0-7 with an optional case-insensitive '0o' prefix as used by modern language literals.
Regex Pattern
^(0o)?[0-7]+$Default flags: i
Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| (0o)? | Optional literal '0o' prefix (case-insensitive due to the i flag), matching modern octal literal syntax like 0o17 |
| [0-7]+ | One or more digits, each restricted to the octal range 0 through 7 |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern validates that a string is a well-formed octal number: an optional '0o' or '0O' prefix followed by one or more digits from 0 to 7. It rejects any digit 8 or 9, letters other than the prefix 'o', and empty input.
Why it works
The optional group (0o)? matches languages like JavaScript, Python, and Rust that spell octal literals with a leading 0o, while making the prefix optional also allows bare digit strings like '17' to validate as octal. The i flag makes the 'o' in the prefix match both upper and lower case. The character class [0-7] excludes 8 and 9, which are not valid octal digits, and the + quantifier requires at least one digit. Anchoring with ^ and $ ensures the whole string, not a substring, is checked.
Common use cases
- Validating octal literals in a code editor or linter for languages that support 0o-prefixed numbers
- Checking Unix file permission strings before passing them to a chmod-style API
- Parsing octal escape sequences or configuration values that use base-8 notation
- Building a base converter tool that accepts octal input in either prefixed or bare form
Edge cases
- A bare digit string like '755' (common for Unix permissions) is accepted even without the 0o prefix
- Mixed case prefixes like '0O17' are accepted because of the case-insensitive i flag
- Digits 8 and 9, as in '789', are correctly rejected since they fall outside the octal range
- An empty string, or just the prefix '0o' with no following digits, is rejected
Limitations
- Does not distinguish between the legacy C-style leading-zero octal notation (e.g. '0755') and a plain decimal string that happens to start with 0, since both look like bare octal digits here
- Does not enforce a maximum value or bit width; add a bounded length if a fixed-size octal value (e.g. 3-digit permissions) is required
- Treats the prefix as fully optional, so callers needing to require the 0o prefix should change (0o)? to 0o
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 |
Language Variants
Production-ready examples in 12 languages.
const octalRegex = /^(0o)?[0-7]+$/i;
console.log(octalRegex.test('0o17')); // trueCommon Mistakes
Using the decimal digit class [0-9] instead of [0-7], which lets invalid octal digits 8 and 9 slip through
Fix: Restrict the character class to [0-7] since octal only has eight valid digits
Requiring the '0o' prefix unconditionally, which rejects legitimate bare octal strings like Unix permission codes '755'
Fix: Make the prefix optional with (0o)? unless the use case specifically requires the modern literal syntax
Forgetting the case-insensitive flag and missing valid uppercase-prefixed literals like '0O17'
Fix: Add the i flag (or its language equivalent) so both '0o' and '0O' are accepted
Performance Notes
- The optional prefix group and bounded character class keep this pattern linear-time with no backtracking risk
- Because [0-7] is a small, non-overlapping character class, the engine can reject invalid digits in constant time per character
- Precompiling the regex once and reusing it avoids repeated compilation overhead in hot loops such as bulk permission-string validation
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |