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})$

This regex matches valid credit card numbers according to the Luhn algorithm for Visa, MasterCard, American Express, and Discover card formats.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the start of the string Start of '4111111111111111'
(?: Non-capturing group start: groups alternatives without capturing Start of Visa pattern
4 Literal digit 4: matches the first digit of Visa cards First digit in '4111111111111111'
[0-9]{12} Exactly 12 digits (0-9): matches the next 12 digits of Visa cards Digits 2-13 in '4111111111111111'
(?: Non-capturing group start: groups optional CVV Start of optional CVV for Visa
[0-9]{3} Exactly 3 digits (0-9): matches the CVV for Visa Last 3 digits in '4111111111111111123'
)? Quantifier: makes the preceding group optional (0 or 1 times) Optional CVV in '4111111111111111'
| Alternation: separates alternative patterns Separator between Visa and MasterCard patterns
5 Literal digit 5: matches the first digit of MasterCard cards First digit in '5500000000000004'
[1-5] Digit class: matches digits 1-5 (second digit of MasterCard) Second digit in '5500000000000004'
[0-9]{14} Exactly 14 digits (0-9): matches the remaining 14 digits of MasterCard Digits 3-16 in '5500000000000004'
| Alternation: separates alternative patterns Separator between MasterCard and Amex patterns
3 Literal digit 3: matches the first digit of American Express cards First digit in '378282246310005'
[47] Digit class: matches digits 4 or 7 (second digit of Amex) Second digit in '378282246310005'
[0-9]{13} Exactly 13 digits (0-9): matches the remaining 13 digits of Amex Digits 3-15 in '378282246310005'
| Alternation: separates alternative patterns Separator between Amex and Discover patterns
3 Literal digit 3: matches the first digit of Discover cards First digit in '30569309025904'
(?: Non-capturing group start: groups second digit options for Discover Start of second digit group for Discover
0[0-5] Digit class: matches 0 followed by 0-5 (second and third digits) Digits 2-3 in '30569309025904'
| Alternation: separates alternative second/third digit patterns Separator between 0[0-5] and [68][0-9]
[68][0-9] Digit class: matches 6 or 8 followed by any digit (second and third digits) Digits 2-3 in '38520000023237'
) Non-capturing group end: ends the second digit options group End of second digit group for Discover
[0-9]{11} Exactly 11 digits (0-9): matches the remaining 11 digits of Discover Digits 4-14 in '30569309025904'
) Non-capturing group end: ends the main alternation group End of all card pattern alternatives
$ Anchor: asserts the end of the string End of '4111111111111111'

Matches

  • 4111111111111
  • 4111111111111123
  • 5500000000000004
  • 378282246310005
  • 30569309025904
  • 38520000023237

Does not match

  • 411111111111
  • 550000000000000
  • 37828224631000
  • 3056930902590
  • 3852000002323
  • 1234567890123456
  • 41111111111111111234

Common pitfalls

  • This regex does not validate the Luhn algorithm (checksum) — it only checks the card number format.
  • The optional CVV for Visa (3 digits) is not part of the standard card number format — this may cause false positives.
  • The regex does not account for spaces or hyphens in card numbers (e.g., '4111 1111 1111 1111').
  • It matches invalid card numbers that pass the format check but fail the Luhn check.

Performance notes

The regex is well-structured with no catastrophic backtracking risks due to the use of non-capturing groups and quantifiers. However, anchoring (^ and $) is critical to prevent partial matches. The alternation is ordered from most common to least common patterns, which can improve matching speed in practice.

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 regex validate credit card numbers using the Luhn algorithm?

No, this regex only checks the format (length and starting digits) of credit card numbers. It does not validate the checksum (Luhn algorithm).

Why does the Visa pattern include an optional 3-digit group at the end?

The optional group (?:[0-9]{3})? is likely intended to match the CVV (Card Verification Value) for Visa cards, which is 3 digits. However, CVV is not part of the standard credit card number format and should not be included in the number itself.

Can this regex match card numbers with spaces or hyphens?

No, this regex does not account for spaces or hyphens. To match such formats, you would need to preprocess the string (remove non-digit characters) or use a more complex pattern with optional separators.

What is the purpose of the non-capturing groups (?:...) in this regex?

Non-capturing groups are used to group subpatterns without creating a capture group. This improves performance and avoids unnecessary memory usage, especially in alternations where capturing is not needed.