Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^[A-Za-z0-9]{4,8}(-[A-Za-z0-9]{4,8}){3,7}$

Matches strings consisting of 4 to 8 alphanumeric characters, followed by 3 to 7 segments of a hyphen and another 4 to 8 alphanumeric characters each.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
[A-Za-z0-9] Character class: matches any uppercase letter, lowercase letter, or digit A, z, 5
{4,8} Quantifier: matches between 4 and 8 occurrences of the preceding token 4 to 8 alphanumeric chars
( Start of a non-capturing group Group start
- Literal hyphen character -
[A-Za-z0-9] Character class: matches any uppercase letter, lowercase letter, or digit A, z, 5
{4,8} Quantifier: matches between 4 and 8 occurrences of the preceding token 4 to 8 alphanumeric chars
) End of the non-capturing group Group end
{3,7} Quantifier: matches between 3 and 7 occurrences of the preceding group 3 to 7 hyphen-separated segments
$ Anchor: asserts position at end of the string End of string

Matches

  • Abc1-Def2-Ghi3-Jkl4
  • a1b2-c3d4-e5f6-g7h8
  • ABCD-EFGH-IJKL-MNOP-QRST-UVWX
  • 1234-5678-9012-3456
  • aBcD-1234-eFgH-5678

Does not match

  • Abc-Def-Ghi-Jkl
  • Abc1-Def2-Ghi3-Jkl4-Mno5-Pqr6-Stu7-Vwx8-Yz
  • Abc1Def2Ghi3Jkl4
  • Abc1-Def2-Ghi3-Jkl4-
  • -Abc1-Def2-Ghi3-Jkl4
  • Abc1-Def2-Ghi3-Jkl4-Mno5

Common pitfalls

  • Forgetting that the total length must be between 20 and 60 characters (4-8 + 3-7 segments of 5-9 chars each).
  • Assuming the pattern allows spaces or other special characters (it only allows alphanumeric and hyphens).
  • Misunderstanding the quantifier ranges: {4,8} applies to the first segment, {3,7} to the groups of segments.
  • Not anchoring with ^ and $, which could lead to partial matches in longer strings.

Performance notes

The pattern is efficient as it uses fixed quantifiers and no nested quantifiers. 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 allow uppercase and lowercase letters and digits only?

Yes, the character class [A-Za-z0-9] explicitly matches uppercase letters, lowercase letters, and digits.

Can the segments have different lengths within the allowed range?

Yes, each segment (including the first) can independently be 4 to 8 characters long.

Is the hyphen required between segments?

Yes, the pattern requires a hyphen between each segment, as indicated by the literal hyphen token '-' inside the group.

What is the minimum and maximum total length of a matching string?

Minimum length: 4 (first segment) + 3 * 5 (3 segments of 4-8 chars + hyphen) = 19. Maximum length: 8 (first segment) + 7 * 9 (7 segments of 4-8 chars + hyphen) = 71.