const regex = new RegExp('SWITCH\\s*([\\d\\-\\s\\:]*)(?:[\\.\\d\\s\\*\\w\\-\\(\\)]*)GROUP\\s*\\d*\\s(\\w*)', 'gm')
const str = `
MSCi MSSBTO01 2019-12-23 11:54:06
ALARM HISTORY
<HIST> MSSBTO01 CMM-0 SWITCH 2019-12-23 11:53:20.42
*** ALARM CMM-0 1A005-00 KICKER
(2691) 2087 LOW TRAFFIC CAPACITY ON CIRCUIT GROUP
01 0784
<HIST> MSSBTO01 CMM-0 SWITCH 2019-12-23 11:53:20.42
*** ALARM CMM-0 1A005-00 KICKER
(2692) 2087 LOW TRAFFIC CAPACITY ON CIRCUIT GROUP
01 0785
<HIST> MSSBTO01 CMM-0 SWITCH 2019-12-23 11:53:20.42
*** ALARM CMM-0 1A005-00 KICKER
(2693) 2087 LOW TRAFFIC CAPACITY ON CIRCUIT GROUP
01 07AC
<HIST> MSSBTO01 CMM-0 SWITCH 2019-12-23 11:53:20.42
*** ALARM CMM-0 1A005-00 KICKER
(2694) 2087 LOW TRAFFIC CAPACITY ON CIRCUIT GROUP
01 07AD
<HIST> MSSBTO01 CMM-0 SWITCH 2019-12-23 11:53:20.42
*** ALARM CMM-0 1A005-00 KICKER
(2695) 2087 LOW TRAFFIC CAPACITY ON CIRCUIT GROUP
01 07AF
END OF ALARM HISTORY
COMMAND EXECUTED
`;
// 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