Advertisement

Pattern library / Strings & Text

Regex to Match Hexadecimal Strings

Matches hex digits with an optional 0x prefix. Useful for color codes, hashes, and binary data in GA4 event parameters.

The pattern
^(0[xX])?[0-9a-fA-F]+$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
deadbeefMatchlowercase hexTry →
0xDEADBEEFMatch0x prefixTry →
0x123Matchshort hexTry →
g123No matchg not hexTry →
0xNo matchno digits after prefixTry →
12 34No matchspace breaks matchTry →

Where to use it

JavaScript

const re = /^(0[xX])?[0-9a-fA-F]+$/;
re.test('0xDEADBEEF');  // true

Python

import re
re.match(r'^(0[xX])?[0-9a-fA-F]+$', 'deadbeef')

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.