const regex = /,"(?<date>\d\d\d\d-\d\d-\d\d)\ (?<time>\d\d:\d\d:\d\d).*\[(?<jail>sshd|recidive|mysqld-auth)\]\ (?<action>[a-zA-z]*)\ (?<ip_address>[\d\.]*)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp(',"(?<date>\\d\\d\\d\\d-\\d\\d-\\d\\d)\\ (?<time>\\d\\d:\\d\\d:\\d\\d).*\\[(?<jail>sshd|recidive|mysqld-auth)\\]\\ (?<action>[a-zA-z]*)\\ (?<ip_address>[\\d\\.]*)', 'gm')
const str = `@timestamp,@message
2021-04-30 18:17:08.504,"2021-04-30 19:17:04,189 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.223 - 2021-04-30 19:17:03"
2021-04-30 18:11:24.504,"2021-04-30 19:11:20,137 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.198 - 2021-04-30 19:11:19"
2021-04-30 18:04:24.504,"2021-04-30 19:04:19,434 fail2ban.filter [100432]: INFO [sshd] Found 221.131.165.56 - 2021-04-30 19:04:19"
2021-04-30 18:03:04.504,"2021-04-30 19:02:59,705 fail2ban.filter [100432]: INFO [sshd] Found 213.171.212.141 - 2021-04-30 19:02:59"
2021-04-30 17:58:11.504,"2021-04-30 18:58:06,901 fail2ban.filter [100432]: INFO [recidive] Found 205.185.119.236 - 2021-04-30 18:58:06"
2021-04-30 17:58:07.132,"2021-04-30 18:58:06,628 fail2ban.actions [100432]: NOTICE [sshd] Ban 205.185.119.236"
2021-04-30 17:58:06.631,"2021-04-30 18:58:06,208 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,206 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,206 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,207 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.381,"2021-04-30 18:58:06,207 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:58:06.380,"2021-04-30 18:58:06,205 fail2ban.filter [100432]: INFO [sshd] Found 205.185.119.236 - 2021-04-30 18:58:05"
2021-04-30 17:57:40.504,"2021-04-30 18:57:35,482 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.143 - 2021-04-30 18:57:35"
2021-04-30 17:41:27.504,"2021-04-30 18:41:23,069 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.135 - 2021-04-30 18:41:22"
2021-04-30 17:40:09.504,"2021-04-30 18:40:05,206 fail2ban.filter [100432]: INFO [sshd] Found 222.187.239.107 - 2021-04-30 18:40:04"
2021-04-30 17:38:16.504,"2021-04-30 18:38:11,847 fail2ban.filter [100432]: INFO [sshd] Found 221.181.185.151 - 2021-04-30 18:38:11"`;
// 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