Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[A-Fa-f0-9]{6}$

Matches a string that is exactly 6 characters long, where each character is a hexadecimal digit (case-insensitive).

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of 'A1b2C3'
[ Start of a character class ...
A-F Range of uppercase hexadecimal letters A to F A, B, C, D, E, F
a-f Range of lowercase hexadecimal letters a to f a, b, c, d, e, f
0-9 Range of digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
] End of the character class ...
{6} Quantifier: exactly 6 occurrences of the preceding element (the character class) Matches 'A1b2C3' but not 'A1b2C' or 'A1b2C34'
$ Anchor: asserts the position at the end of the string End of 'A1b2C3'

Matches

  • A1b2C3
  • 123456
  • abcdef
  • ABCDEF
  • 000000
  • fEdCbA
  • 1a2B3c

Does not match

  • A1b2C
  • A1b2C34
  • G1b2C3
  • A1b2C3!
  • A1b2C3
  • A1b2C 3
  • A1b2C 3

Common pitfalls

  • Forgetting the anchors ^ and $, which would allow strings longer than 6 characters to match if they contain a 6-character hex substring.
  • Assuming the pattern matches any hexadecimal string without considering the exact length requirement.
  • Overlooking case insensitivity; the pattern explicitly includes both uppercase and lowercase ranges.
  • Not accounting for non-hexadecimal characters (e.g., 'G', 'z', or symbols) which will not match.

Performance notes

This pattern is efficient as it uses a character class with a fixed quantifier. No catastrophic backtracking risk. Ensure input strings are not excessively long to avoid unnecessary processing.

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 strings with whitespace or special characters?

No. The pattern only matches strings consisting of exactly 6 hexadecimal digits (0-9, A-F, a-f). Any whitespace or special characters will cause the match to fail.

Can this pattern be used to validate hexadecimal color codes?

Yes, but only if the color code is exactly 6 characters long (e.g., '#A1b2C3' would not match without the '#' prefix). For full hex color validation, use a pattern like '^#?[A-Fa-f0-9]{6}$'.

Why is the {6} quantifier used instead of specifying the exact length in another way?

The {6} quantifier is the most straightforward and efficient way to specify that exactly 6 characters must match the preceding element (the character class). Alternatives like ^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$ are functionally equivalent but less concise.