Credit Card Regex Tester
Test and validate credit-card patterns with this specialized regex tester. Includes tested patterns and real-time matching.
Recommended Credit-card Pattern
``regex
^\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}$
`
Test Examples
Valid matches:
- 4111111111111111
4111-1111-1111-11114111 1111 1111 1111
Invalid (should not match):
4111111111111114111-1111-1111abcd-efgh-ijkl-mnop
Pattern Explanation
Matches 16-digit card numbers with optional spaces or dashes
Alternative Patterns
1. ^\d{16}$
2. ^(?:\d{4}[- ]?){4}$
How to Use
1. The pattern above is preloaded—or enter your own
2. Add test strings to validate
3. See real-time match highlights
4. Copy the pattern for your code
Regex Quick Reference
| Symbol | Meaning | Example | ||
|---|---|---|---|---|
| \d | Any digit | \d{3} matches "123" | ||
| \w | Word character | \w+ matches "hello" | ||
| + | One or more | a+ matches "aaa" | ||
| * | Zero or more | a* matches "" or "aaa" | ||
| ? | Optional | colou?r matches "color" | ||
| ^ | Start of string | ^Hello | ||
| $ | End of string | world$ | ||
| [abc] | Character class | [aeiou] matches vowels | ||
| (a | b) | Alternation | (cat | dog) matches either |
The Pattern
`regex
^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$
`
Flags: gm — m makes ^ and $ match at each line break, so the pattern can be tested against a list.
Broken Down
| Part | What it does | |
|---|---|---|
4\d{12}(?:\d{3})? | Visa — 13 or 16 digits starting with 4 | |
5[1-5]\d{14} | Mastercard — 16 digits, 51–55 | |
3[47]\d{13} | American Express — 15 digits, 34 or 37 | |
| 6(?:011 | 5\d{2})\d{12} | Discover |
Tested Against Real Input
| Input | Result |
|---|---|
4111111111111111 | ✅ matches |
5500000000000004 | ✅ matches |
340000000000009 | ✅ matches |
1234567890123456 | ❌ no match |
The Important Caveat
Matching the shape is not validation — run the Luhn checksum as well. And never log, store or transmit a card number you matched; PCI DSS scope follows the data.
Using It
`javascript
const pattern = /^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$/gm;
// Test a single value — reset lastIndex first if the pattern is global
pattern.lastIndex = 0;
const isValid = pattern.test(value);
// Or find every match in a block of text
const matches = [...text.matchAll(pattern)];
`
A global regex carries lastIndex between calls, so reusing one across test() calls
returns alternating results. Either drop the g flag for validation or reset it each time.
Anchors, Greediness and Backtracking
Three behaviours account for most regex surprises, and this pattern shows all three.
Anchors. ^ and $ pin the match to the start and end of the input. Without them,
\d{3} matches the 123 inside abc123def. With m in the flags — as here — they
pin to each *line* instead, which is what lets one pattern be tested against a list.
Greediness. .* takes as much as it can and gives back only when forced; .*? takes
as little as possible. On , the pattern <.*> matches the whole string and
<.*?> matches just .