const regex = /(?P<request_date>^[\w\/\:]+)\s+(?P<request_offset>[\d\+]+)\s+\[(?P<request_id>.+)\]\s+(?P<request_inout>[\-\>\<]+)\s+((?P<request_method>[GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH]+)?\s+(?P<request_path>[^;][^;][a-zA-Z0-9\/\_\-\.\=\@\:\%\+\~\#\?\&\{\}\[\]\|]+)\s+(?P<request_protocol>.+)\s+|(?P<request_code>\d+)\s+(?P<request_filetype>.+)\s+(?P<request_duration>[\d\w]+)\s+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?P<request_date>^[\\w\\\/\\:]+)\\s+(?P<request_offset>[\\d\\+]+)\\s+\\[(?P<request_id>.+)\\]\\s+(?P<request_inout>[\\-\\>\\<]+)\\s+((?P<request_method>[GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH]+)?\\s+(?P<request_path>[^;][^;][a-zA-Z0-9\\\/\\_\\-\\.\\=\\@\\:\\%\\+\\~\\#\\?\\&\\{\\}\\[\\]\\|]+)\\s+(?P<request_protocol>.+)\\s+|(?P<request_code>\\d+)\\s+(?P<request_filetype>.+)\\s+(?P<request_duration>[\\d\\w]+)\\s+)', 'gm')
const str = `14/May/2021:09:37:39 +0000 [715153] -> GET /content/ams/healthcheck/regent.html HTTP/1.1
14/May/2021:09:37:39 +0000 [2585977] <- 200 text/html;charset=UTF-8 6ms
14/May/2021:10:37:39 +0100 [1502141] -> GET /libs/granite/csrf/token.json HTTP/1.1
14/May/2021:09:37:39 +0000 [715152] <- 200 text/html;charset=UTF-8 6ms
14/May/2021:11:52:22 +0000 [11097977] -> GET /content/onehub_nfz/ru/ru/model-overview/caddy.html?tc=oa-[B]_[Caddy]_[All]_[None]_[BMM]_[RU]_[Eval]_Yandex_Search_AON_Paid_Search-Yandex-cpc-Search-banner&kw={PHRASE}&utm_source=yandex&utm_medium=cpc&utm_campaign=[B]_[Caddy]_[All]_[None]_[BMM]_[RU]_[Eval]_Yandex_Search_AON_Paid_Search&utm_term={PHRASE}&utm_content=s_{SRC}|cid_45783621|gid_{GBID}|aid_8728578416|pid_{PHRASE_EXPORT_ID}|rid_{PARAM126}|p_{POS}|pty_{PTYPE} HTTP/1.1
`;
// 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