const regex = /(Send: (N\d+\s+)?(G0|G1|G2|G3)).*|(Recv: ok)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(Send: (N\\d+\\s+)?(G0|G1|G2|G3)).*|(Recv: ok)', 'gm')
const str = `Send: N15316 G2 X132.005 Y144.699 I2.530 J2.266 E1967.48230*80
Recv: ok
Send: N15317 G2 X130.872 Y146.440 I8.090 J6.504 E1967.55146*83
Recv: ok
Send: N15318 G3 X128.033 Y150.765 I-83.048 J-51.420 E1967.72359*80
Recv: ok
Send: N15319 G2 X126.450 Y152.896 I19.401 J16.065 E1967.81191*88
Recv: ok
Send: N15320 G3 X123.581 Y158.450 I-52.577 J-23.641 E1968.01996*86
Recv: ok
Send: N3830 G1 F1800 X137.124 Y134.295 E1247.73559*2
Recv: ok
Send: N3831 G0 F7200 X137.525 Y134.131*112
Recv: ok
Send: N3832 G1 F1800 X173.8 Y170.406 E1249.44186*15
Recv: ok
Send: N3833 G0 F7200 X173.957 Y169.997*119
Recv: == T:209.96 /210.00 == B:60.21 /60.00 @:79 B@:127
Recv: ok
Send: N3834 G1 F1800 X137.95 Y133.991 E1251.13549*53
Recv: ok
Send: N3835 G0 F7200 X138.404 Y133.878*122
Recv: ok
Send: N3836 G1 F1800 X174.132 Y169.606 E1252.81603*7
Recv: ok
Send: N3837 G0 F7200 X174.242 Y169.151*121
Recv: == T:209.73 /210.00 == B:60.66 /60.00 @:85 B@:0
Recv: ok
Send: N3838 G1 F1800 X138.857 Y133.766 E1254.48043*2
Recv: ok
Send: N3839 G0 F7200 X139.311 Y133.654*116
Recv: == T:209.77 /210.00 == B:60.69 /60.00 @:84 B@:0
Recv: ok
Send: N3840 G1 F1800 X174.354 Y168.697 E1256.12874*13
Recv: ok
Send: N3841 G0 F7200 X174.465 Y168.242*123
Recv: ok
Send: N3842 G1 F1800 X139.775 Y133.553 E1257.76043*11
Recv: ok
Send: N3843 G0 F7200 X140.258 Y133.469*119
Recv: == T:209.92 /210.00 == B:60.65 /60.00 @:79 B@:0
Recv: ok
`;
// 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