Numbers & Finance · In-Browser Regular Expression Lab
Credit Card Regex — Validate Visa (13/16) and Mastercard (16) Numbers
Detects Visa and Mastercard credit card format with optional space or hyphen delimiters.
Flags:
//none
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:4111111111111111
Test Samples Verification Matrix
Expected Valid Samples (Should Match)
4111111111111111 Pass
5500000000000004 Pass
2221000000000000 Pass
Expected Invalid Samples (Must Reject)
378282246310005 Rejected (Pass)
411111111111 Rejected (Pass)
9999999999999999 Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|2(?:22[1-9]|2[3-9][0-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})$/;
const text = "4111111111111111";
const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);
if (isMatch) {
const matches = text.match(pattern);
console.log("Matches:", matches);
}Regular Expression Token Breakdown
Matches Visa starting with 4 (13 or 16 digits) and Mastercard starting with 51-55 or 2221-2720 (16 digits). Each component of the regular expression serves a distinct syntactical purpose as mapped below:
| Token Pattern | Name / Component | Function & Rule |
|---|---|---|
| 4[0-9]{12}(?:[0-9]{3})? | Visa Format | Starts with 4, total 13 or 16 digits. |
| 5[1-5][0-9]{14} | Mastercard Legacy | Starts with 51-55, total 16 digits. |
| 2[2-7]...[0-9]{12} | Mastercard 2-Series | Starts with 2221-2720, total 16 digits. |
Frequently Asked Questions
Should I also run the Luhn algorithm?
Related Regular Expressions
Browse All 40 PatternsNumbers & Finance
IBAN Bank Account Matcher
Validates canonical IBAN structure: 2-letter country code, 2-digit checksum, and up to 30 alphanumeric characters.
/^[A-Z]{2}[0-9]{2}[A-Z0-9]{4}[0-9]{7}([A-Z0-9]?){0,16}$/Test regex
Numbers & Finance
US Phone Number Format
Flexible US phone number validator matching standard parentheses, hyphens, and spaces.
/^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})$/Test regex
Code & Markup
Hexadecimal Color Code
Matches CSS hex colors in #RGB, #RRGGBB, and #RRGGBBAA (with alpha transparency) formats.
/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/Test regex