const regex = /^foo\.bar\.lib(?:\..*)?(?<!unsafe)\.\w+$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^foo\\.bar\\.lib(?:\\..*)?(?<!unsafe)\\.\\w+$', 'gm')
const str = `foo.bar.lib.Main
foo.bar.lib.impl.Main
foo.bar.libxxx.Main
foo.bar.libxxx.impl.Main
foo.bar.lib.unsafe.Unsafe
foo.bar.lib.unsafe.Unsafe
foo.bar.libxxx.unsafe.Unsafe
foo.bar.lib.util.Main
foo.bar.lib.impl.util.Main
foo.bar.libxxx.util.Main
foo.bar.libxxx.impl.util.Main
foo.bar.lib.util.unsafe.Unsafe
foo.bar.lib.util.unsafe.Unsafe
foo.bar.libxxx.util.unsafe.Unsafe`;
// 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