A Mastercard regex is short enough to copy and long enough to misread. This guide makes its range coverage reviewable: compare every four-digit prefix with the same numeric profile used by the site’s tools, then test length and normalization independently.
The exact local interval
The configured profile uses 51–55 and 2221–2720, with 16 digits in total. The Mastercard generator exercises those ranges. Its boundary fixtures are useful for form QA; this guide adds an exhaustive equivalence check for an implementation change.
The two series should not be merged into “starts with 2 or 5.” In the four-digit space, the older series covers 5100 through 5599. The newer series covers 2221 through 2720. Each contains 500 four-digit prefixes. That arithmetic explains the test domain, not a claim about why a network chose its allocation.
| Case | Input / condition | Expected result |
|---|---|---|
| mc-2220 | 2220 + twelve zeros (format only) | false |
| mc-2221 | 2221 + twelve zeros (format only) | true |
| mc-2720 | 2720 + twelve zeros (format only) | true |
| mc-2721 | 2721 + twelve zeros (format only) | false |
| mc-5100 | 5100 + twelve zeros (format only) | true |
| mc-5600 | 5600 + twelve zeros (format only) | false |
The suffixes in this matrix are deliberately zeros. These cases test format, so a true result does not promise a passing checksum.
Read the regex as five intervals
export function mastercardFormat(input) {
const { status, digits } = normalizeInput(input);
if (status !== 'ok' || digits.length !== 16) return false;
// Format only: the checksum is a separate inspectCard result.
return /^(?:5[1-5][0-9]{14}|(?:222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})$/.test(digits);
}The 2-series alternatives expand to 2221–2229, 2230–2299, 2300–2699, 2700–2719 and 2720. They cover a continuous interval with no gap. Twelve more digits complete each four-digit prefix. The 51–55 branch instead starts with two digits and appends fourteen.
Normalization is the shared strict function: spaces and ASCII hyphens are allowed, letters and newlines are rejected, and numeric arguments are not converted to strings. The explicit length check also prevents a regex from accepting a prefix-only value.
Prove equivalence with the running profile
test('Mastercard regex agrees with the shared profile for every 4-digit prefix', () => {
for (let n = 0; n < 10000; n += 1) {
const pan = String(n).padStart(4, '0') + '000000000000';
const expected = prefixCandidates(pan, profile).brand === 'mastercard';
assert.equal(mastercardFormat(pan), expected, `prefix ${pan.slice(0, 4)}`);
}
for (const input of ['222100000000000', '22210000000000000',
'2221000000000000\n', '2221x000000000000', 2221000000000000]) {
assert.equal(mastercardFormat(input), false);
}
});This loop checks all 10,000 prefixes from 0000 through 9999. Its expected classification comes from card-profiles.json through the numeric candidate selector, independently of the regex being tested. If the profile changes but this regex does not, the comparison fails instead of publishing contradictory explanations.
Exhaustive prefix coverage does not test every 16-digit string. Separate assertions cover wrong length, trailing newline, an inserted letter and numeric input. The brand detection guide explains the shared selection rules; the card validator adds the checksum result.
Partial input and neighboring families
While a user types 2, then 22, then 222, the final interval is unresolved. Do not display an invalid-card error merely because a prefix is unfinished. At 2220, the complete boundary falls outside the Mastercard range; at 2221, it matches.
Maestro has its own configured ranges and lengths. A number starting with 56 can belong to a different local candidate; it should not be accepted by this Mastercard regex. Use the Maestro scenarios and network length reference for those rules. The Amex guide shows a different error: correct brand with a failed checksum.
Neither a brand range nor a guessed six-digit slice identifies a current issuing product. The BIN/IIN guide covers that distinction, and the PAN structure guide shows how an assumed boundary changes the account portion.
Run or adapt the comparison
Download the complete offline guide project and extract it. Node.js 22 or later runs:
node --test guide-examples.test.mjs
node guide-demo.mjs mastercard
Keep the exhaustive check when replacing the regex with another implementation. If your provider supports a different profile, version that input and update the expected behavior deliberately; do not relax a range merely to make one failing fixture pass.