Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$

Matches valid credit card numbers according to major card issuer patterns (Visa, Mastercard, Amex, Discover, Diners Club, JCB, etc.).

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of '4111111111111111'
(?: Start of non-capturing group for alternation N/A
4[0-9]{12} Visa: starts with 4, followed by 12 digits 4111111111111
(?:[0-9]{3})? Optional group: 3 digits (Visa 16-digit variant) 4111111111111111
| Alternation (OR operator) N/A
5[1-5][0-9]{14} Mastercard: starts with 51-55, followed by 14 digits 5555555555554444
| Alternation (OR operator) N/A
3[47][0-9]{13} Amex: starts with 34 or 37, followed by 13 digits 378282246310005
| Alternation (OR operator) N/A
3(?:0[0-5]|[68][0-9])[0-9]{11} Diners Club: starts with 300-305 or 36-38, followed by 11 digits 30569309025904
| Alternation (OR operator) N/A
6(?:011|5[0-9]{2})[0-9]{12} Discover: starts with 6011 or 65, followed by 12 digits 6011111111111117
| Alternation (OR operator) N/A
(?:2131|1800|35\d{3})\d{11} JCB: starts with 2131, 1800, or 35 followed by 3 digits, then 11 digits 3530111333300000
) End of non-capturing group for alternation N/A
$ Anchor: asserts position at end of the string End of '4111111111111111'

Matches

  • 4111111111111
  • 4111111111111111
  • 5555555555554444
  • 378282246310005
  • 30569309025904
  • 6011111111111117
  • 3530111333300000

Does not match

  • 1234567890123456
  • 411111111111111
  • 5105105105105100
  • 341111111111111
  • 601111111111111
  • 3528000700000000

Common pitfalls

  • Forgetting to escape backslashes in JavaScript strings (e.g., using \d instead of \d).
  • Assuming this pattern validates Luhn checksum (it only checks issuer patterns).
  • Not anchoring the pattern (^ and $) leading to partial matches.
  • Mistaking the Visa pattern for 13 or 16 digits without considering the optional group.

Performance notes

The alternation is well-structured with no catastrophic backtracking risk. Each branch is mutually exclusive and quantifiers are bounded. Anchoring ensures full-string matching.

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 validate the Luhn checksum?

No, this pattern only checks the issuer-specific digit patterns. Luhn validation requires additional logic.

Why is the Visa pattern split into two parts?

Visa cards can be 13 or 16 digits long. The pattern first matches 13 digits, then optionally matches 3 more to cover 16-digit cards.

How do I make this pattern case-insensitive?

This pattern does not need case-insensitivity as it only matches digits. If extended to include alphanumeric patterns, add the 'i' flag in JavaScript.