const regex = /(?<=HTTP\/1.0"\s)(301)(?=\s5024)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=HTTP\\\/1.0"\\s)(301)(?=\\s5024)', 'gm')
const str = `11.22.33.44 - - [17/Aug/2019:11:24:01 -0400] "GET /posts/posts/explore HTTP/1.0" 301 5024 "http://www.someurl.blah/faq.php" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_12_5) AppleWebKit/5310 (KHTML, like Gecko) Chrome/29.0.801.0 Safari/5310"
`;
// 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