const regex = /^(?P<name>[\w\-\.]+)-(?P<version>v?\d+(\.\d+)?(\.\d+)?(-[\w\-]+(\.[\w\-]+)*)?(\+[\w\-]+(\.[\w\-]+)*)?)(?P<ext>\.[\w\-]+)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?P<name>[\\w\\-\\.]+)-(?P<version>v?\\d+(\\.\\d+)?(\\.\\d+)?(-[\\w\\-]+(\\.[\\w\\-]+)*)?(\\+[\\w\\-]+(\\.[\\w\\-]+)*)?)(?P<ext>\\.[\\w\\-]+)$', 'gm')
const str = `ansi-escapes-4.3.1.tgz
babel-eslint-10.1.0.tgz
chance-1.1.7.tgz
cli-cursor-3.1.0.tgz
eslint-7.13.0.tgz
faker-5.1.0.tgz
forever-monitor-3.0.1.tgz
fs-extra-9.0.1.tgz
lorem-ipsum-2.0.3.tgz
micromatch-4.0.2.tgz
mongoose-5.10.13.tgz
puppeteer-5.4.1.tgz
puppeteer-extra-3.1.15.tgz
puppeteer-extra-plugin-stealth-2.6.3.tgz
random-email-1.0.3.tgz
random-useragent-0.5.0.tgz
random-words-1.1.1.tgz
slice-ansi-4.0.0.tgz
uuid-8.3.1.tgz
validator-13.1.17.tgz
wrap-ansi-7.0.0.tgz
font-awesome-5.11.2.tgz
react-dom-16.13.1.tgz
bootstrap-4.5.2.tgz
moment-duration-format-2.3.2.tgz
imagesloaded-4.1.tgz
flickity-2.0.tgz
lomster-1.tgz
unicorn-1.2.3-alpha.10.beta.0.tgz
rainbow-v1.1.1.tgz
object.assign-4.1.4.tgz
hello_world-0.0.3.tgz
typescript-package-1-1.0.2.tgz
`;
// 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