const regex = /^(?<date>\d{2}\.\d{2}\.\d{4})\s(?<time>\d{2}:\d{2}:\d{2}\.\d{3})\s{2}(?<msg>.+?(?=\s\d))\s(?<id>.{4})\s(?<name>.+?(?=$|\d{2}\.))(?<comment>.*$)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<date>\\d{2}\\.\\d{2}\\.\\d{4})\\s(?<time>\\d{2}:\\d{2}:\\d{2}\\.\\d{3})\\s{2}(?<msg>.+?(?=\\s\\d))\\s(?<id>.{4})\\s(?<name>.+?(?=$|\\d{2}\\.))(?<comment>.*$)', 'gm')
const str = `04.08.2024 06:41:14.775 Получено время 2447 Название устройства №2 04.08.2024 6:41:15 - синхронизация не требуется
04.08.2024 06:43:14.786 Получено время 22B3 Название устройства №3 04.08.2024 6:43:16 - время получения ответа превышает допуск
04.08.2024 11:47:01.967 Истекли попытки отправить запрос 234C Название устройства №4
05.08.2024 14:03:55.149 Получено время 2356 Название устройства №1 05.08.2024 14:03:57 - время получения ответа превышает допуск
05.08.2024 14:03:56.796 Получено время 2356 Название устройства №1 05.08.2024 14:03:58 - требуется синхронизация
05.08.2024 14:03:58.920 Установлена коррекция времени 2356 Название устройства №1`;
// 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