Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[a-f0-9]{64}$

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

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of string
[ Start of a character class Start of character class
a-f Range: matches any lowercase 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 End of character class
{64} Quantifier: matches exactly 64 occurrences of the preceding element (the character class [a-f0-9]) 64 hexadecimal characters
$ Anchor: asserts the position at the end of the string End of string

Matches

  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
  • aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899
  • ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

Does not match

  • 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde
  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0
  • 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefg

Common pitfalls

  • Case sensitivity: fails to match uppercase hexadecimal letters (A-F).
  • Length requirement: fails if the string is shorter or longer than 64 characters.
  • Whitespace: fails if the string contains leading or trailing whitespace (anchors ^ and $ prevent this).
  • Invalid characters: fails if the string contains any character not in the range [a-f0-9].

Performance notes

No catastrophic backtracking risk due to fixed quantifier {64} and simple character class. Anchors ^ and $ ensure the regex engine does not scan beyond the start/end of the string. Efficient for typical use cases.

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 uppercase hexadecimal letters?

No, the character class [a-f0-9] only matches lowercase letters 'a' to 'f' and digits '0' to '9'.

Can I use this pattern to match a SHA-256 hash?

Yes, SHA-256 hashes are 64-character hexadecimal strings, and this pattern will match them if they are in lowercase.

What if the string has leading or trailing spaces?

The anchors ^ and $ ensure the entire string must match the pattern, so leading or trailing spaces will cause the match to fail.