const regex = new RegExp('^(?P<ip>\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}) - - \\[(?P<date>.*)\\] "(?P<request>.*?)" (?P<status>\\d*) (?P<size>\\d*) "(?P<referer>.*?)" "(?P<user_agent>.*?)".*$', 'gm')
const str = `192.0.102.40 - - [09/Apr/2018:06:26:33 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
192.0.102.40 - - [09/Apr/2018:06:26:33 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
198.199.64.224 - - [09/Apr/2018:06:26:33 -0400] "POST /blog/wp-cron.php?doing_wp_cron=1523269593.5797910690307617187500 HTTP/1.1" 499 0 "-" "WordPress/4.9.5; https://ivoah.net/blog"
192.0.102.40 - - [09/Apr/2018:06:26:33 -0400] "HEAD /blog/ HTTP/1.1" 200 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
192.0.102.40 - - [09/Apr/2018:06:31:32 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
192.0.102.40 - - [09/Apr/2018:06:31:32 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
198.199.64.224 - - [09/Apr/2018:06:31:33 -0400] "POST /blog/wp-cron.php?doing_wp_cron=1523269893.1204290390014648437500 HTTP/1.1" 499 0 "-" "WordPress/4.9.5; https://ivoah.net/blog"
192.0.102.40 - - [09/Apr/2018:06:31:33 -0400] "HEAD /blog/ HTTP/1.1" 200 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
192.0.102.40 - - [09/Apr/2018:06:36:39 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
192.0.102.40 - - [09/Apr/2018:06:36:39 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
198.199.64.224 - - [09/Apr/2018:06:36:39 -0400] "POST /blog/wp-cron.php?doing_wp_cron=1523270199.4308791160583496093750 HTTP/1.1" 499 0 "-" "WordPress/4.9.5; https://ivoah.net/blog"
192.0.102.40 - - [09/Apr/2018:06:36:39 -0400] "HEAD /blog/ HTTP/1.1" 200 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
47.52.68.60 - - [09/Apr/2018:06:37:08 -0400] "POST /blog/2016/05/13/mate-on-raspberry-pi/?share=email&nb=1 HTTP/1.1" 302 5 "https://ivoah.net/blog/2016/05/13/mate-on-raspberry-pi/?share=email&nb=1" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2)"
47.52.68.60 - - [09/Apr/2018:06:37:09 -0400] "GET /blog/2016/05/13/mate-on-raspberry-pi/?shared=email HTTP/1.1" 200 30602 "https://ivoah.net/blog/2016/05/13/mate-on-raspberry-pi/?share=email&nb=1" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2)"
94.23.33.67 - - [09/Apr/2018:06:37:43 -0400] "GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
94.23.33.67 - - [09/Apr/2018:06:37:43 -0400] "GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
94.23.33.67 - - [09/Apr/2018:06:37:44 -0400] "GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0" 404 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
94.23.33.67 - - [09/Apr/2018:06:37:44 -0400] "GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
94.23.33.67 - - [09/Apr/2018:06:37:45 -0400] "GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0" 301 184 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
94.23.33.67 - - [09/Apr/2018:06:37:45 -0400] "GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0" 404 570 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
192.0.102.40 - - [09/Apr/2018:06:41:44 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
192.0.102.40 - - [09/Apr/2018:06:41:44 -0400] "HEAD /blog HTTP/1.1" 301 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
198.199.64.224 - - [09/Apr/2018:06:41:44 -0400] "POST /blog/wp-cron.php?doing_wp_cron=1523270504.8351509571075439453125 HTTP/1.1" 499 0 "-" "WordPress/4.9.5; https://ivoah.net/blog"
192.0.102.40 - - [09/Apr/2018:06:41:44 -0400] "HEAD /blog/ HTTP/1.1" 200 0 "-" "jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)"
207.244.149.74 - - [09/Apr/2018:06:45:38 -0400] "GET /wp-content/plugins/wp-formgenerator/assets/js/simpleslider.js HTTP/1.1" 404 341 "https://ivoah.net/wp-content/plugins/wp-formgenerator/assets/js/simpleslider.js" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"
54.36.148.188 - - [09/Apr/2018:06:45:44 -0400] "GET /wp/audio/05042011.m4a HTTP/1.1" 301 184 "-" "Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)"
`;
// 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