const regex = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<preRelease>(?:[a-zA-Z1-9][a-zA-Z\d]*|0\d*[a-zA-Z][a-zA-Z\d]*|0)(?:\.(?:[a-zA-Z1-9][a-zA-Z\d]*|0\d*[a-zA-Z][a-zA-Z\d]*|0))*))?(?:\+(?<metadata>(?:[a-zA-Z\d-]*)(?:\.(?:[a-zA-Z\d-]*))*))?$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<major>0|[1-9]\\d*)\\.(?<minor>0|[1-9]\\d*)\\.(?<patch>0|[1-9]\\d*)(?:-(?<preRelease>(?:[a-zA-Z1-9][a-zA-Z\\d]*|0\\d*[a-zA-Z][a-zA-Z\\d]*|0)(?:\\.(?:[a-zA-Z1-9][a-zA-Z\\d]*|0\\d*[a-zA-Z][a-zA-Z\\d]*|0))*))?(?:\\+(?<metadata>(?:[a-zA-Z\\d-]*)(?:\\.(?:[a-zA-Z\\d-]*))*))?$', 'gm')
const str = `0.1.0
01.2.1
1.2.0
1.0.0
1.01.0
1.0.01
1.0.0-0df34.ahgdfhsdf.0005a56tg
1.0.0-0054.df4gf
1.0.0-005a4.df4gf
1.0.0-005a4.df4gf+adffbff.sdfg.sdfg.adf78df7d8df7
1.0.0-005a4.df4gf+adffbff.sdfg.sdfg.00000000.dfsg.sdf-sfg.sdfg-sdfg.sgf
900.10.85494
0900.10.54`;
// 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