Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?:d{4}-){3}d{4}$

Matches a string that represents a 16-digit number formatted as four groups of four digits separated by hyphens (e.g., 1234-5678-9012-3456).

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '1234-5678-9012-3456'
(?: Start of a non-capturing group (Javascript-specific) Start of '(?:\d{4}-)'
\d Digit character class (matches any digit 0-9) Digit '1' in '1234'
{4} Quantifier: exactly 4 times Applies to '\d' to match '1234'
- Literal hyphen character Hyphen '-' in '1234-'
) End of non-capturing group End of '(?:\d{4}-)'
{3} Quantifier: exactly 3 times Applies to the non-capturing group to repeat it 3 times
\d Digit character class (matches any digit 0-9) Digit '1' in '1234'
{4} Quantifier: exactly 4 times Applies to '\d' to match '3456'
$ Anchor: asserts position at end of the string End of '1234-5678-9012-3456'

Matches

  • 1234-5678-9012-3456
  • 0000-0000-0000-0000
  • 9999-8888-7777-6666

Does not match

  • 1234567890123456
  • 1234-5678-9012-345
  • 1234-5678-9012-34567
  • 1234-5678-9012-345a
  • -1234-5678-9012-3456
  • 1234-5678-9012-3456-

Common pitfalls

  • Forgetting to escape the hyphen in the character class (though not needed here as it's literal)
  • Misunderstanding the non-capturing group vs capturing group
  • Assuming the pattern matches numbers without hyphens
  • Overlooking the anchors ^ and $ which enforce full-string matching

Performance notes

This pattern is efficient with no risk of catastrophic backtracking due to fixed quantifiers and no alternations or variable-length quantifiers. The non-capturing group is used for grouping without capturing, which is optimal for performance.

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

Why use a non-capturing group (?:...) instead of a capturing group (...)?

Non-capturing groups are more efficient as they do not store the matched substring in memory for backreferences or extraction, which is unnecessary in this pattern.

Can this pattern match strings with leading or trailing spaces?

No, because the anchors ^ and $ assert positions at the very start and end of the string, excluding any leading or trailing whitespace.

Does this pattern validate the digits themselves (e.g., check if they are valid numbers)?

No, it only checks the format (four groups of four digits separated by hyphens). It does not validate the numeric value or range of the digits.