const regex = /.(.*)(]\s)(.[a-zA-Z]*)(..)(.*)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('.(.*)(]\\s)(.[a-zA-Z]*)(..)(.*)', 'gm')
const str = `[1518-08-11 00:47] falls asleep
[1518-03-09 00:07] falls asleep
[1518-06-29 00:41] wakes up
[1518-05-27 23:58] Guard #743 begins shift
[1518-05-19 00:43] falls asleep
[1518-03-17 00:33] falls asleep
[1518-07-12 00:49] falls asleep
[1518-11-03 00:15] wakes up
[1518-06-05 00:57] wakes up
[1518-11-07 00:24] falls asleep
[1518-07-06 00:12] falls asleep
[1518-05-26 23:59] Guard #907 begins shift
[1518-06-01 00:28] wakes up
[1518-11-09 00:04] falls asleep
`;
// 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