BytePane

Hexadecimal Number Regex Pattern

Validates hexadecimal numbers with an optional 0x prefix. Use it for numeric hex literals; CSS color hex codes need a separate #RGB/#RRGGBB pattern.

Reviewed May 31, 2026. Privacy model: tool input is processed in your browser and is not uploaded to BytePane servers.

#
Numbers
Beginner
Difficulty
Universal
Language
none
Flags
// Regular Expression
/^(0x)?[0-9a-fA-F]+$/

Live Regex Tester

Pattern Breakdown

^(0x)?[0-9a-fA-F]+$
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

const regex = /^(0x)?[0-9a-fA-F]+$/;
const test = "0xFF00FF";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'^(0x)?[0-9a-fA-F]+$'
test = "0xFF00FF"
match = re.search(pattern, test)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`^(0x)?[0-9a-fA-F]+$`)
    fmt.Println(re.MatchString("0xFF00FF")) // true
}

Common Use Cases

0x literalsmemory addressesbyte stringsCSS color validationbinary data representation

Match Examples

InputResult
0xFF00FFMatch
0xGGNo Match

Hexadecimal regex variants for 0x, CSS colors, bytes, and fixed length

The base pattern validates a numeric hex literal such as 0xFF00FF or FF00FF. Tighten the pattern when you need CSS colors, byte pairs, fixed-width IDs, or case-insensitive 0x/0X prefixes.

VariantPatternUse it when
Optional 0x or 0X prefix^(?:0[xX])?[0-9a-fA-F]+$Use for code literals, memory addresses, and config values where both 0x and 0X should be accepted.
CSS hex color^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$Use for #RGB, #RRGGBB, and #RRGGBBAA color inputs. Keep this separate from general hexadecimal numbers.
Exactly 8 hex characters^[0-9a-fA-F]{8}$Use for fixed-width checksums, compact IDs, ARGB colors, or packed 32-bit values.
Hex byte sequence^(?:[0-9a-fA-F]{2})(?:[\s:-]?[0-9a-fA-F]{2})*$Use for byte strings such as FF 00 A4, FF:00:A4, or FF-00-A4.

Edge cases to test

0xFF00FFMatch

Matches the base pattern because the 0x prefix is optional.

0XFF00FFNo match in base pattern

Use the 0[xX] variant if uppercase X should be valid.

#FF00FFNo match in base pattern

Use the CSS hex color variant for # prefixes.

0xNo match

At least one hex digit is required after the optional prefix.

0xGGNo match

G is not a hexadecimal digit.

About the Hexadecimal Number Regex

Validates hexadecimal numbers with an optional 0x prefix. Use it for numeric hex literals; CSS color hex codes need a separate #RGB/#RRGGBB pattern.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The hexadecimal number pattern is classified as <strong>beginner</strong> difficulty in the <strong>numbers</strong> category. It works in all major programming languages.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the Hexadecimal Number regex pattern?

Validates hexadecimal numbers with an optional 0x prefix. Use it for numeric hex literals; CSS color hex codes need a separate #RGB/#RRGGBB pattern.

How do I use the Hexadecimal Number regex?

Use the pattern /^(0x)?[0-9a-fA-F]+$/ in your code. In JavaScript: new RegExp('^(0x)?[0-9a-fA-F]+$', ''). Test it above with your own input.

What does this Hexadecimal Number regex match?

This pattern matches: "0xFF00FF". It does NOT match: "0xGG". 0x literals, memory addresses, byte strings, CSS color validation, binary data representation.

Is the Hexadecimal Number regex beginner-friendly?

This pattern is rated Beginner. It uses basic regex syntax and is easy to understand.

What languages support the Hexadecimal Number regex?

This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.

Can I modify the Hexadecimal Number regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools