Semantic Version (SemVer) in JS
Match semantic version strings like 1.2.3, 1.2.3-beta.1, or 1.2.3+build.42.
Try it in the JS tester →Pattern
regexJS
(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.]+)?(?:\+[\w.]+)? (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(?:-[\\w.]+)?(?:\\+[\\w.]+)?", "g");
const input = "1.0.0";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
Three numeric components (no leading zeros) separated by dots. Optional pre-release label after - and optional build metadata after + per the SemVer 2.0.0 spec.
Examples
Input
1.0.0Matches
1.0.0
Input
2.3.1-beta.1Matches
2.3.1-beta.1
Input
1.0.0+build.42Matches
1.0.0+build.42