const regex = /^(?!.*\-{2}.*)(?!.*\+{2}.*)(?!.*\.{2}.*)(?P<major>(?!0)(\d*)|([0[^\d]))((?>\.)(?P<minor>\g<1>))\.(?P<patch>\g<minor>)(?![\+\-][^a-zA-Z0-9])(\g<2>(\-(?P<release>[a-zA-Z0-9\.-]+)))?(\+(?P<build>\g<9>))?$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?!.*\\-{2}.*)(?!.*\\+{2}.*)(?!.*\\.{2}.*)(?P<major>(?!0)(\\d*)|([0[^\\d]))((?>\\.)(?P<minor>\\g<1>))\\.(?P<patch>\\g<minor>)(?![\\+\\-][^a-zA-Z0-9])(\\g<2>(\\-(?P<release>[a-zA-Z0-9\\.-]+)))?(\\+(?P<build>\\g<9>))?$', 'gm')
const str = `#good
0.1.2
1.2.3
10.20.3
1.2.3-alpha.23-pre
12.12.3-123.hexagon+dontmakemecompileplea.se
1.2.3-alpha-dev.51-something+mybuild-1-4-1975-clang
4.3.22+mybuild
4.1.405+hexa.13331-objectfiles
# bad
01.2.3
1.02.3
2.3.04
a.1.1
1.a.1
1.1.a
1.2.3-rele..ase+build
1.2.3-release-something+..build
1.2.3-rele--ase+build
1.2.3-release-something+--build
1.2.3-release++build
1.2.3+-release-something-build`;
// 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