DevTools
Back to Regex Tester

Credit Card Number Regex Pattern

Match and validate credit card numbers (Visa, MasterCard, Amex, Discover) using regex. Includes Luhn-check-ready patterns with optional separators.

The Pattern

/\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

How It Works

TokenMeaning
\bWord 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)

Test Cases

4111111111111111Match
4111-1111-1111-1111Match
5500 0000 0000 0004Match
371449635398431Match
6011111111111117Match
1234567890123456No match
411111No match

Code Examples

javascript
const 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"]
python
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']

Common Use Cases

Related Patterns

Want to test this pattern with your own data?

Try this pattern in Regex Tester →