Match and validate credit card numbers (Visa, MasterCard, Amex, Discover) using regex. Includes Luhn-check-ready patterns with optional separators.
/\b(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))[- ]?\d{4}[- ]?\d{4}[- ]?\d{1,4}\b/g| Token | Meaning |
|---|---|
\b | Word boundary to prevent partial matches |
4\d{3} | Visa: starts with 4, followed by 3 digits |
5[1-5]\d{2} | MasterCard: starts with 51-55 |
3[47]\d{2} | Amex: starts with 34 or 37 |
6(?:011|5\d{2}) | Discover: starts with 6011 or 65 |
[- ]? | Optional dash or space separator between groups |
\d{4} | Group of 4 digits |
\d{1,4} | Final group of 1 to 4 digits (Amex has 15 digits total) |
4111111111111111Match4111-1111-1111-1111Match5500 0000 0000 0004Match371449635398431Match6011111111111117Match1234567890123456No match411111No matchconst ccRegex = /\b(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))[- ]?\d{4}[- ]?\d{4}[- ]?\d{1,4}\b/g;
const text = "Cards: 4111-1111-1111-1111 and 5500 0000 0000 0004";
const matches = text.match(ccRegex);
console.log(matches); // ["4111-1111-1111-1111", "5500 0000 0000 0004"]import re
cc_regex = r'\b(?:4\d{3}|5[1-5]\d{2}|3[47]\d{2}|6(?:011|5\d{2}))[- ]?\d{4}[- ]?\d{4}[- ]?\d{1,4}\b'
text = "Cards: 4111-1111-1111-1111 and 5500 0000 0000 0004"
matches = re.findall(cc_regex, text)
print(matches) # ['4111-1111-1111-1111', '5500 0000 0000 0004']Want to test this pattern with your own data?
Try this pattern in Regex Tester →