const regex = /^(?=(?P<LE10>(?:0[0-9]|1[0-9]|2[0-8]|3[0-7]|4[0-6]|5[0-5]|6[0-4]|7[0-3]|8[0-2]|9[0-1])))?(?:(?:X|(?P>LE10))-){9}(?:XXX|XX\d|X?(?P>LE10)|(?:19|28|37|46|55|64|7|3|82|91)[X\d])$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?=(?P<LE10>(?:0[0-9]|1[0-9]|2[0-8]|3[0-7]|4[0-6]|5[0-5]|6[0-4]|7[0-3]|8[0-2]|9[0-1])))?(?:(?:X|(?P>LE10))-){9}(?:XXX|XX\\d|X?(?P>LE10)|(?:19|28|37|46|55|64|7|3|82|91)[X\\d])$', 'gm')
const str = `X-91-55-72-X-X-X-90-71-91X
X-91-55-72-X-X-X-90-82-XXX
X-91-55-72-X-X-X-90-82-XX7
X-91-55-72-X-X-X-90-82-X91
X-91-55-72-X-X-X-90-82-90
X-91-55-72-X-X-X-90-82-X91
X-91-55-72-X-X-X-90-82-917
X-91-72-X-X-X-90-82-917
Number of delimiters (8) incorrect
X-91-55-7-X-X-X-90-82-91X
7 in first 9 is invalid
X-91-55-72-X-X-X-X94-82-91X
X94 in first 9 is invalid
X-91-55-72-X-X-X-90-82-X
last = X is not valid
X-91-55-72-X-X-X-90-82-XX
last = XX is not valid
X-91-55-72-X-X-X-90-82-XXXX
last = XXXX is not valid
X-91-55-72-X-X-X-90-82-X92
last = X92 is not valid
X-91-55-72-X-X-X-90-82-94
last = 94 is not valid
X-91-55-72-X-X-X-90-82-62X
last = 62X is not valid
X-91-55-72-X-X-X-90-82-927
last = 927 is not valid
`;
// 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