const regex = /((?<DATE>\d{1,2}\/\d{1,2}\/\d{2})\s[A-Z0-9]{4}\/\d{7}(?<LE>\s[A-Z]{4}\d{6}\-\d).*(?<LPreis>\s\d+,\d{2})\s+06[\s\S]*?(?<GGF>(?=(Dangerous goods:).\d,\d{2})|)(.|\n)*?(?<SUB>(?=(SUBSIDIES)\s+\d+,\d{2})|)(.|\n)*?(?<ADR>(?=(ADR SURCHARGE CH)\s+\d+,\d{2})|).*)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((?<DATE>\\d{1,2}\\\/\\d{1,2}\\\/\\d{2})\\s[A-Z0-9]{4}\\\/\\d{7}(?<LE>\\s[A-Z]{4}\\d{6}\\-\\d).*(?<LPreis>\\s\\d+,\\d{2})\\s+06[\\s\\S]*?(?<GGF>(?=(Dangerous goods:).\\d,\\d{2})|)(.|\\n)*?(?<SUB>(?=(SUBSIDIES)\\s+\\d+,\\d{2})|)(.|\\n)*?(?<ADR>(?=(ADR SURCHARGE CH)\\s+\\d+,\\d{2})|).*)', 'gm')
const str = `* 19/11/21 SSS2/3000000 AAAA300007-0 028/2 782 4000 690,00 06
5527315
Dangerous goods: 150,00
Additional performances:
- SUPPL.REDUCTION CH GOVERNMENT SUBSIDIES 5,00
- ADR SURCHARGE CH 2,00
19/11/21 BBB2/6000000 BBBB200006-0 222/2 715 2620 690,00 06
5545628
Additional performances:
- SUPPL.REDUCTION CH GOVERNMENT SUBSIDIES 150,00`;
// 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