Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[a-fA-F0-9]{32}$

Matches a string that is exactly 32 characters long, consisting only of hexadecimal digits (0-9, a-f, A-F).

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string '' (start of string)
[ Start of a character class N/A
a-f Range: matches any lowercase letter from 'a' to 'f' a, b, c, d, e, f
A-F Range: matches any uppercase letter from 'A' to 'F' A, B, C, D, E, F
0-9 Range: matches any digit from '0' to '9' 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
] End of a character class N/A
{32} Quantifier: exactly 32 occurrences of the preceding element (the character class) 32 hexadecimal characters
$ Anchor: asserts position at end of the string '' (end of string)

Matches

  • 0123456789abcdefABCDEF0123456789ab
  • ffffffffffffffffffffffffffffffff
  • 00000000000000000000000000000000
  • 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d

Does not match

  • 0123456789abcdefABCDEF0123456789a
  • 0123456789abcdefABCDEF0123456789abcd
  • 0123456789abcdefABCDEF0123456789ab!
  • 0123456789abcdefABCDEF0123456789ab
  • 0123456789abcdefABCDEF0123456789ab

Common pitfalls

  • Forgetting the anchors (^ and $) may allow strings with extra characters to match.
  • Confusing hexadecimal ranges (a-fA-F) with other alphanumeric ranges.
  • Assuming the pattern matches UUIDs without the hyphens (standard UUIDs are 36 chars with hyphens).
  • Overlooking case sensitivity (e.g., 'G' or 'g' would not match).

Performance notes

This pattern is efficient with no risk of catastrophic backtracking. The character class is simple, and the fixed quantifier {32} ensures linear time complexity. Anchors ensure no unnecessary backtracking occurs.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this pattern match UUIDs?

No, standard UUIDs are 36 characters long (32 hex digits with 4 hyphens). This pattern matches exactly 32 hexadecimal characters without any separators.

Can I make this case-insensitive instead of using a-fA-F?

Yes, in JavaScript you could use the 'i' flag (e.g., /^[a-f0-9]{32}$/i) to avoid writing both cases explicitly.

What if I want to match a string with exactly 32 hex digits but allow leading/trailing whitespace?

Remove the anchors (^ and $) and trim the string in JavaScript, or use \s* at the start and end (e.g., /^\s*[a-fA-F0-9]{32}\s*$/).