Skip to main content
Regexflux

Semantic Version (SemVer)

intermediate

Matches semantic version strings like 1.2.3 or 2.0.0-beta.1.

semverversionnpmdevelopment

Pattern

/\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?(?:\+[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?\b/g

Try It

0 of 8 strings matched

Explanation

Matches SemVer format: major.minor.patch with optional pre-release identifiers (after -) and build metadata (after +). No leading zeros in numeric identifiers.

Test Strings

Matching

  • 1.0.0
  • v2.3.4-beta.1
  • 0.1.0+build.123
  • 10.20.30-alpha.1+sha.45678

Non-matching

  • 1.2
  • 01.2.3
  • abc
  • v

Language Compatibility

LanguageSupport
JSFull support
PYTHONFull support
JAVAFull support
PHPFull support
GOLimited support (ASCII-only word boundaries)

Code Snippets

const regex = /\bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?(?:\+[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?\b/g;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);

Common Variations

Loose version

\d+\.\d+(?:\.\d+)?

Matches major.minor with optional patch

Related Patterns