const regex = /S304JB0...(?<Total1>\d+).(?<Total2>\d+).(?<Total3>\d+).(?<Total4>\d+).(?<Total5>\d+).(?<Total6>\d+).(?<Total7>\d+)(?:R01(?<Row1>\d+))?(?:R02(?<Row2>\d+))?(?:R03(?<Row3>\d+))?(?:R04(?<Row4>\d+))?(?:R05(?<Row5>\d+))?(?:R06(?<Row6>\d+))?(?:R07(?<Row7>\d+))?(?:R08(?<Row8>\d+))?(?:R09(?<Row9>\d+))?(?:R10(?<Row10>\d+))?(?:R11(?<Row11>\d+))?(?:R12(?<Row12>\d+))?(?:R13(?<Row13>\d+))?(?:R14(?<Row14>\d+))?(?:R15(?<Row15>\d+))?/ig;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('S304JB0...(?<Total1>\\d+).(?<Total2>\\d+).(?<Total3>\\d+).(?<Total4>\\d+).(?<Total5>\\d+).(?<Total6>\\d+).(?<Total7>\\d+)(?:R01(?<Row1>\\d+))?(?:R02(?<Row2>\\d+))?(?:R03(?<Row3>\\d+))?(?:R04(?<Row4>\\d+))?(?:R05(?<Row5>\\d+))?(?:R06(?<Row6>\\d+))?(?:R07(?<Row7>\\d+))?(?:R08(?<Row8>\\d+))?(?:R09(?<Row9>\\d+))?(?:R10(?<Row10>\\d+))?(?:R11(?<Row11>\\d+))?(?:R12(?<Row12>\\d+))?(?:R13(?<Row13>\\d+))?(?:R14(?<Row14>\\d+))?(?:R15(?<Row15>\\d+))?', 'ig')
const str = `12*000000000**S304JB01811*8*0*8*4*4*34R0332R152~~~
12*000000000**S304JB01811*9*0*4*3*4*224R023R032R10234R1325~~~
`;
// 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