const regex = /(?P<release>(?:0|[1-9])\d*(?:\.(?:0|[1-9]\d*)){2})(?:-(?P<pre>(?P<prel>a|b|rc)(?P<pren>(?:0|[1-9])\d*)))?(?:\+(?P<local>[a-z\d]+(?:[-_\.][a-z\d]+)*))?/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?P<release>(?:0|[1-9])\\d*(?:\\.(?:0|[1-9]\\d*)){2})(?:-(?P<pre>(?P<prel>a|b|rc)(?P<pren>(?:0|[1-9])\\d*)))?(?:\\+(?P<local>[a-z\\d]+(?:[-_\\.][a-z\\d]+)*))?', 'gm')
const str = `Valid PEP440 Verions
Final versions
0.9
0.9.1
0.9.2
0.9.10
0.9.11
1.0.1
2.0.1
Version epochs
1.0
1.1
2.0
2013.10
2014.04
2013.10
2014.04
1!1.0
1!1.1
1!2.0
Examples of compliant version schemes
Simple "major.minor" versioning:
0.1
0.2
0.3
1.0
1.1
Simple "major.minor.micro" versioning:
1.1.0
1.1.1
1.1.2
1.2.0
"major.minor" versioning with alpha, beta and candidate pre-releases:
0.9
1.0-a1
1.0-a2
1.0.7-b1
1.0.34-rc1
1.0
1.1.0-a1
"major.minor" versioning with developmental releases, release candidates and post-releases for minor corrections:
0.9
1.0.dev1
1.0.dev2
1.0.dev3
1.0.dev4
1.0c1
1.0c2
1.0
1.0.post1
1.1.dev1
Date based releases, using an incrementing serial within each year, skipping zero:
2012.1
2012.2
2012.3
2012.15
2013.1
2013.2
The following example covers many of the possible combinations:
1.0.dev456
1.0a1
1.0a2.dev456
1.0a12.dev456
1.0a12
1.0b1.dev456
1.0b2
1.0b2.post345.dev456
1.0b2.post345
1.0rc1.dev456
1.0rc1
1.0
1.0+abc.5
1.0+abc.7
1.0+5
1.0.post456.dev34
1.0.post456
1.1.dev1`;
// 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