const regex = new RegExp('([Gg]0?[01]) *(([XxYyZz]) *(-?\\d+.?\\d*)) *(([XxYyZz]) *(-?\\d+.?\\d*))? *(([XxYyZz]) *(-?\\d+.?\\d*))?', 'gm')
const str = `G1 X110.406 Y109.680 E6.01498
G1 X110.768 Y109.508 E6.02854
G1 X111.167 Y109.404 E6.04252
G1 X111.596 Y109.379 E6.05707
G1 X112.009 Y109.439 E6.07118
`;
// 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