regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match a Credit Card Number

Copy the pattern, paste text below, and watch it highlight card numbers by brand. Below the tester: what each branch matches, and why regex is only a format check.

Regex pattern

/\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})\b/g

This matches the digit format of Visa, Mastercard, Amex, and Discover card numbers. It is a format check only and does NOT verify the Luhn checksum — add a Luhn check in code to catch typos.

/ /

What this pattern matches

This matches the digit format of the four major card brands. It is a format check only — it does not verify the Luhn checksum or that a card is real.

How it works, part by part

4[0-9]{12}(?:[0-9]{3})? — Visa: starts with 4, 13 or 16 digits.

5[1-5][0-9]{14} — Mastercard: starts 51–55, 16 digits.

3[47][0-9]{13} — American Express: starts 34 or 37, 15 digits.

6(?:011|5[0-9]{2})[0-9]{12} — Discover.

Validate it for real: the Luhn checksum

The regex above only confirms the digits look like a card. Every real card number ends in a check digit computed with the Luhn algorithm, which catches almost all single-digit typos. Run this after the format match:

def luhn_ok(number):
    digits = [int(d) for d in number if d.isdigit()][::-1]
    total = 0
    for i, d in enumerate(digits):
        if i % 2 == 1:          # every second digit from the right
            d *= 2
            if d > 9:
                d -= 9
        total += d
    return total % 10 == 0

luhn_ok("4111111111111111")  # True
luhn_ok("4111111111111112")  # False (typo)
function luhnOk(number){
  const d = [...number].filter(c => /\d/.test(c)).map(Number).reverse();
  let sum = 0;
  d.forEach((n, i) => { if (i % 2) { n *= 2; if (n > 9) n -= 9; } sum += n; });
  return sum % 10 === 0;
}
luhnOk("4111111111111111"); // true
luhnOk("4111111111111112"); // false

Format regex + Luhn together reject both wrong-shaped and mistyped numbers — but neither proves a card exists or has funds. Only a payment processor does that.

Important

Use this to detect or redact card-shaped numbers, never to validate payments. Real validation needs the Luhn algorithm and a payment processor. Do not log or store raw card numbers.

Use it in your code

Python
import re
text = "Visa 4111111111111111, Mastercard 5500005555555559, Amex 340000000000009, Discover 6011000000000004. Not a card: 1234 5678."
pattern = r'''\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})\b'''
for m in re.finditer(pattern, text):
    print(m.group())
JavaScript
const text = "Visa 4111111111111111, Mastercard 5500005555555559, Amex 340000000000009, Discover 6011000000000004. Not a card: 1234 5678.";
const re = new RegExp(String.raw`\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})\b`, "g");
console.log(text.match(re));

FAQ

Does it check the Luhn checksum?

No. Regex only checks the digit format. Add a Luhn check in code to catch typos.

Can I use it to redact cards from logs?

Yes — that is a good use. Match and replace with a masked value.