const regex = /^.+\/(.+?)[\W|$]/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^.+\\\/(.+?)[\\W|$]', 'gm')
const str = `/var/log/audit/audit.log
/var/log/audit/audit.log.1
/var/log/boot.log
/var/log/btmp
/var/log/btmp-20170401
/var/log/chrony
/var/log/cron
/var/log/cron-20170326
/var/log/cron-20170402
/var/log/cron-20170410
/var/log/cron-20170416
/var/log/anaconda
./var/log/anaconda/anaconda.log
./var/log/anaconda/ifcfg.log
./var/log/anaconda/journal.log
./var/log/anaconda/ks-script-XcavhD.log
./var/log/anaconda/packaging.log
./var/log/anaconda/program.log
./var/log/anaconda/storage.log
./var/log/anaconda/syslog
./var/log/anaconda/X.log
./var/log/audit
./var/log/audit/audit.log
./var/log/audit/audit.log.1
/var/log/httpd/error_log;
`;
// 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