JavaScript-compatible regex reference

Semantic Version Regex (SemVer) & Tester

Validate semantic-version strings with numeric MAJOR.MINOR.PATCH components plus optional prerelease identifiers and build metadata. The pattern rejects leading zeros in numeric version components while allowing standard dot-separated prerelease and build identifiers.

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-]+)*))?$

Flags: none

Live test vectors

Test the Semantic Version regex

Enter one value per line. Each line is tested against the whole-string pattern using the browser JavaScript RegExp engine.

PASS1.0.0
PASS2.1.3-beta.1
PASS0.1.0+build.42
PASS1.0.0-rc.1+sha.abc123
FAIL1.0
FAIL01.2.3
FAIL1.2.3-
FAILv1.2.3

Quick answer

A SemVer validator should require exactly MAJOR.MINOR.PATCH, reject leading zeros in numeric identifiers, and separately handle optional prerelease and build metadata. A loose \d+\.\d+\.\d+ pattern misses those rules.

Common use cases

  • Package and release validation
  • CI/CD version checks
  • Migration and dependency tooling

Pitfalls and limitations

  • !A leading v, as in v1.2.3, is common in Git tags but is not part of the semantic-version string itself.
  • !Numeric major, minor, patch, and numeric prerelease identifiers must not contain leading zeros.
  • !Regex validates syntax; comparing SemVer precedence still requires semantic comparison logic.

Known-valid examples

1.0.02.1.3-beta.10.1.0+build.421.0.0-rc.1+sha.abc123

Known-invalid examples

1.001.2.31.2.3-v1.2.3

Continue testing and debugging

Frequently asked questions

Does this pattern accept prerelease versions?

Yes. Values such as 2.0.0-beta, 2.0.0-beta.1, and 2.0.0-rc.1 are supported.

Does build metadata affect version precedence?

The regex only validates the syntax. If you need to sort or compare versions, use SemVer-aware comparison logic rather than string comparison.

Why does v1.2.3 fail?

The leading v is a common tag prefix, but it is not part of the SemVer version string. Strip the prefix before validating if your workflow uses v-prefixed tags.