Semantic version regex
MAJOR.MINOR.PATCH without leading zeros, with an optional pre-release after a - and build metadata after a +, as specified by semver.org. Copy the pattern, with 4 inputs it accepts and 6 it rejects, each checked against it at build time.
Part of developer tools.
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/Flags: none
Open in the testerWhat the semantic version regex accepts and rejects
Each result below is the output of the pattern itself, run on the input when this page was built — not a claim copied next to it. A wrong verdict stops the build.
| Input | Result | Why |
|---|---|---|
| 1.0.0 | Passes | a plain release |
| 0.1.0 | Passes | a zero major version |
| 2.10.3-beta.1 | Passes | a pre-release |
| 1.0.0-rc.1+build.5 | Passes | pre-release and build metadata |
| 1.0 | Fails | no patch number |
| v1.0.0 | Fails | a v prefix |
| 01.0.0 | Fails | a leading zero |
| 1.0.0- | Fails | an empty pre-release |
| 1.0.0-01 | Fails | a numeric pre-release with a leading zero |
| ^1.2.0 | Fails | a range, not a version |
Limits
- This is the pattern published with the SemVer 2.0.0 specification. Its capture groups give the major, minor, patch, pre-release and build parts in that order — open it in the tester to see them.
- A leading v (v1.2.3), common in git tags, is not part of SemVer and is refused. Strip it before matching rather than loosening the pattern.
- Ranges and constraints (^1.2.0, ~1.2, >=1.0.0 <2) are a different grammar; npm and Composer each have their own.
Try it on your own input
Open this pattern in the regex tester with every example above on its own line. The link adds the g and m flags so that ^ and $ hold per line and every line is tested: the highlighted lines are the ones marked as passing here. Paste your own data in place of the examples — it is matched in your browser and never uploaded.
Other common regexes
- Email address
- URL
- UUID
- IPv4 address
- Hex color
- ISO 8601 date
- 24-hour time
- URL slug
- Phone number (E.164)
- US ZIP code
- MAC address
Or open the regex tester empty and bring your own pattern.