const regex = /(?<x_client_ip>.*?)\t-\t-\t.*?\t(?<http_status>.*?)\t(?<content_length>.*?)\t(?<referer>.*?)\t(?<user_agent>.*?)\t(?<method>.*?)\t(?<uri>.*?)\t(?<query_string>.*?)\t(?<protocol>.*?)\t(?<duration>.*?)\t-\t-\t-\t(?<id>.*)\t(?<client_ip>\d+\.\d+\.\d+\.\d+)\t/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<x_client_ip>.*?)\\t-\\t-\\t.*?\\t(?<http_status>.*?)\\t(?<content_length>.*?)\\t(?<referer>.*?)\\t(?<user_agent>.*?)\\t(?<method>.*?)\\t(?<uri>.*?)\\t(?<query_string>.*?)\\t(?<protocol>.*?)\\t(?<duration>.*?)\\t-\\t-\\t-\\t(?<id>.*)\\t(?<client_ip>\\d+\\.\\d+\\.\\d+\\.\\d+)\\t', 'g')
const str = `111.111.11.11 - - [17/Mar/2017:05:49:45 -0400] 302 231 "https://test.test.com/test/test/viewTest" "Mozilla/5.0 (Windows NT 10.0; WOW64) webWebKit/537.36 (KHTML, like Gecko) Web/56.0.2924.87 Safari/537.36" GET "/favicon.ico" "" HTTP/1.1 5236 - - - RES58cbb1390a02287220001fc146464dds 111.211.111.111 `;
// 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