gettoolz.app

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.

Pattern^(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-]+)*))?$
JavaScript literal/^(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-]+)*))?$/

What 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.

Inputs tested against the Semantic version regex
InputResultWhy
1.0.0Passesa plain release
0.1.0Passesa zero major version
2.10.3-beta.1Passesa pre-release
1.0.0-rc.1+build.5Passespre-release and build metadata
1.0Failsno patch number
v1.0.0Failsa v prefix
01.0.0Failsa leading zero
1.0.0-Failsan empty pre-release
1.0.0-01Failsa numeric pre-release with a leading zero
^1.2.0Failsa 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

Or open the regex tester empty and bring your own pattern.