const regex = /^
(?:4[0-9]{12}(?:[0-9]{3})?
|
5[1-5][0-9]{14}
|
6(?:011|5[0-9][0-9])[0-9]{12}
|
3[47][0-9]{13}
|
3(?:0[0-5]|[68][0-9])[0-9]{11}
|
(?:2100|2131|1800|35\d{3})\d{11})
$/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^
(?:4[0-9]{12}(?:[0-9]{3})?
|
5[1-5][0-9]{14}
|
6(?:011|5[0-9][0-9])[0-9]{12}
|
3[47][0-9]{13}
|
3(?:0[0-5]|[68][0-9])[0-9]{11}
|
(?:2100|2131|1800|35\\d{3})\\d{11})
$', 'gmi')
const str = `#American Express
370517943574132
372714451742486
370010255141385
341263547614307
343874494387669
#VISA
4024007125780444
4439944519233615
4658355677043536
4532926168018906
4532249806735728
#MasterCard
5524097521691644
5367170623993901
5553103980950937
5549194987582424
5141794881796756
#JCB 15 digits
180078244412845
210013400722277
210082510016250
180056142071970
210043823226606
#JCB
3158822586903214
3088687202983378
3158899851849561
3096803356450490
3337852908456769
#Dinners Club
30193567772121
30131361923813
30198560976769
30260244203356
36297440059376
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions