const regex = /(?<date>\d+\-\d+\-\d+).(?<time>\d+:\d+:\d+).(?<timetaken>\d+).(?<cip>\d+\.\d+\.\d+\.\d+).(?<scstatus>\d+).(?<scaction>\S+).(?<scbytes>\d+).(?<csbytes>\d+).(?<csmethod>\S+).(?<csurischeme>\S+).(?<cshost>\S+).(?<csuriport>\d+).(?<csuripath>\S+).(?<csuriquery>\S+).(?<csusername>\S+).(?<csauthgroup>\S+).(?<shierarchy>\S+).(?<ssuppliername>\S+).(?<rscontenttype>\S+).(?<csreferer>\S+)."?(?<cs_user_agent>.+)"?./gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<date>\\d+\\-\\d+\\-\\d+).(?<time>\\d+:\\d+:\\d+).(?<timetaken>\\d+).(?<cip>\\d+\\.\\d+\\.\\d+\\.\\d+).(?<scstatus>\\d+).(?<scaction>\\S+).(?<scbytes>\\d+).(?<csbytes>\\d+).(?<csmethod>\\S+).(?<csurischeme>\\S+).(?<cshost>\\S+).(?<csuriport>\\d+).(?<csuripath>\\S+).(?<csuriquery>\\S+).(?<csusername>\\S+).(?<csauthgroup>\\S+).(?<shierarchy>\\S+).(?<ssuppliername>\\S+).(?<rscontenttype>\\S+).(?<csreferer>\\S+)."?(?<cs_user_agent>.+)"?.', 'gm')
const str = `2014-03-27 12:39:32 20 10.71.15.207 304 TCP_HIT 367 1470 GET http www.computerworld.com 80 /elqNow/elqFCS.js - - - - 23.196.74.53 application/x-javascript http://www.computerworld.com/s/article/9247206/Gameover_malware_takes_aim_at_Monster.com_and_CareerBuilder.com "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)" OBSERVED "Technology/Internet" - 163.252.254.201 23.44.202.53 52809
`;
// 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