const regex = /(?<timeHour>\d{1,2}):(?<timeMinute>\d{2})(?:\:(?<timeSecond>\d{2}))?\s+up\s+(?:(?<upDays>\d+)\s+days?,\s+)?\b(?:(?<upHours>\d+):)?(?<upMinutes>\d+)(?:\s+min?)?,\s+.+?(?<load1>\d+\.\d+),?\s+(?<load2>\d+\.\d+),?\s+(?<load3>\d+\.\d+)
/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<timeHour>\\d{1,2}):(?<timeMinute>\\d{2})(?:\\:(?<timeSecond>\\d{2}))?\\s+up\\s+(?:(?<upDays>\\d+)\\s+days?,\\s+)?\\b(?:(?<upHours>\\d+):)?(?<upMinutes>\\d+)(?:\\s+min?)?,\\s+.+?(?<load1>\\d+\\.\\d+),?\\s+(?<load2>\\d+\\.\\d+),?\\s+(?<load3>\\d+\\.\\d+)
', 'gm')
const str = `15:05 up 1 day, 10:40, 4 users, load averages: 1.68 1.30 1.30
12:27:57 up 25 min, load average: 0.33, 0.33, 0.28
12:27:58 up 1:26, load average: 5.44, 5.29, 5.26
00:36:55 up 16 days, 26 min, load average: 0.83, 0.75, 0.76
`;
// 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