Hexadecimal Number Regex
Validates a base-16 (hexadecimal) number that must be written with an explicit '0x' or '0X' prefix, followed by one or more hex digits.
Regex Pattern
^0[xX][0-9a-fA-F]+$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| 0[xX] | Literal '0' followed by either lowercase 'x' or uppercase 'X', matching the required hex prefix |
| [0-9a-fA-F]+ | One or more hexadecimal digits: 0-9 and both cases of a-f |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern requires a string to start with '0x' or '0X' and be followed by one or more valid hexadecimal digits (0-9, a-f, A-F). Strings without the prefix, or containing characters outside the hex digit set, are rejected.
Why it works
The literal segment 0[xX] mandates the prefix used by virtually every mainstream programming language for hex literals, while allowing either letter case. The character class [0-9a-fA-F] covers all sixteen valid hex digit symbols in both letter cases, and the + quantifier requires at least one digit after the prefix. Anchoring with ^ and $ prevents partial matches, such as a hex-looking substring embedded inside a longer string.
Common use cases
- Validating hex literals in a code editor, linter, or template engine before evaluation
- Checking user-entered memory addresses, color codes, or byte values that require the 0x prefix
- Parsing configuration files or CLI flags that accept hexadecimal numeric input
- Filtering log output for well-formed hex addresses versus malformed tokens
Edge cases
- Mixed-case hex digits like '0xDeadBEEF' are accepted since both cases are included in the character class
- A bare hex string without the prefix, such as '1A3F', is rejected by design; strip or require the prefix consistently upstream
- The prefix alone with no following digits, '0x', is rejected because + requires at least one hex digit
- Invalid characters like 'g' or 'G', which fall outside a-f/A-F, correctly cause a rejection, e.g. '0xGG'
Limitations
- Requires the 0x/0X prefix unconditionally; if bare hex strings (e.g. from a color picker without '#') should also validate, use an alternation or a separate pattern
- Does not enforce a specific digit count, so both very short and very long hex numbers are accepted; add a bounded quantifier for fixed-width values like 32-bit addresses
- Does not validate that the resulting numeric value fits within a particular integer size without additional length constraints
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 hexRegex = /^0[xX][0-9a-fA-F]+$/;
console.log(hexRegex.test('0x1A3F')); // trueCommon Mistakes
Forgetting that hex digits are case-insensitive and only matching lowercase a-f, rejecting valid uppercase input like '0xFF'
Fix: Include both cases in the character class, [0-9a-fA-F], or apply the case-insensitive flag
Requiring the 0x prefix when the input source (e.g. a color picker) never includes it
Fix: Decide up front whether the prefix is mandatory and use a separate bare-hex pattern like ^[0-9a-fA-F]+$ if not
Confusing this pattern with one for CSS hex colors, which have fixed lengths of 3, 4, 6, or 8 digits and use a '#' prefix instead of '0x'
Fix: Use a dedicated hex-color pattern for CSS colors rather than reusing a generic numeric hex pattern
Performance Notes
- The fixed-length prefix followed by a single bounded character class gives this pattern linear-time matching with no backtracking risk
- Anchoring at both ends lets the engine reject non-hex strings as soon as the prefix check fails
- For very large batches of hex validation, precompiling the regex once outside the loop avoids repeated compilation cost
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |