const regex = new RegExp('(?P<remote_addr>(?:^|\\b(?<!\\.))(?:1?\\d\\d?|2[0-4]\\d|25[0-5])(?:\\.(?:1?\\d\\d?|2[0-4]\\d|25[0-5])){3}(?=$|[^\\w.]))\\s-\\s(?P<remote_usr>-|[a-z_][a-z0-9_]{0,30})\\s(?P<date_time>\\[(?P<date>[0-2][0-9]\\/\\w{3}\\/[12]\\d{3}):(?P<time>\\d\\d:\\d\\d:\\d\\d).*\\])\\s(?P<request>\\"(?P<req_method>GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH)\\s(?P<req_uri>\\/[^\\s]*)\\s(?P<http_ver>HTTP/\\d\\.\\d)\\")\\s(?P<status>\\d{3})\\s(?P<body_byte_sent>\\d+)\\s\\"(?P<http_referer>[^\\s]+)\\"\\s\\"(?P<user_agent>[^\\"]+)\\"\\s\\"(?P<forward_for>[^\\"]+)\\"', '')
const str = `110.233.182.14 - - [03/Aug/2017:00:52:02 +0900] "GET / HTTP/1.1" 200 9017 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "110.233.182.14"`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
if ((m = regex.exec(str)) !== null) {
// 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