const regex = /^(?<date>\d{4}\-\d{2}\-\d{2})\s+(?<time>\d{2}\:\d{2}\:\d{2})\s+(?<sip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?<csmethod>GET|POST|PUT|PATCH|DELETE)\s+(?<csuristem>.+?)\s+(?<csuriquery>.+?)\s+(?<sport>\d{1,3})\s+(.+?)\s+(?<cip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?<csUseragent>.+?)\s+(?<scstatus>\d{1,3})\s+(?<scsubstatus>\d{1,3})\s+(?<scwin32status>\d+)\s+(?<scbytes>\d+)\s+(?<csbytes>\d+)\s+(?<timetaken>\d+)?$/;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<date>\\d{4}\\-\\d{2}\\-\\d{2})\\s+(?<time>\\d{2}\\:\\d{2}\\:\\d{2})\\s+(?<sip>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s+(?<csmethod>GET|POST|PUT|PATCH|DELETE)\\s+(?<csuristem>.+?)\\s+(?<csuriquery>.+?)\\s+(?<sport>\\d{1,3})\\s+(.+?)\\s+(?<cip>\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\s+(?<csUseragent>.+?)\\s+(?<scstatus>\\d{1,3})\\s+(?<scsubstatus>\\d{1,3})\\s+(?<scwin32status>\\d+)\\s+(?<scbytes>\\d+)\\s+(?<csbytes>\\d+)\\s+(?<timetaken>\\d+)?$', '')
const str = `2014-05-06 10:27:17 10.200.40.56 GET / - 443 - 10.200.28.7 Mozilla/5.0+(compatible;+MSIE+9.0;+Windows+NT+6.1;+WOW64;+Trident/5.0) 401 2 2148074254 1880 428 109`;
// 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