HomeRegex LibrarySemantic Versioning (SemVer) Regex
100% Client-Side β€’ 0 B Data Leaves Browser
validation

Semantic Versioning (SemVer) Regex

Validates official Semantic Versioning 2.0.0 strings including pre-release and build metadata.

Regular Expression Pattern:
/^v?(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-]+)*))?$/
Valid Match Example:
v2.4.1-alpha.3+build.2026
Invalid / Non-Match Example:
2.4

Regex Syntax Breakdown & Explanation

  • β€’^v? : Optional leading v prefix
  • β€’(0|[1-9]\d*) : Major version number without leading zeros
  • β€’\.(0|[1-9]\d*) : Minor version number
  • β€’\.(0|[1-9]\d*) : Patch version number
  • β€’(?:-...) : Optional pre-release identifiers

Implementation in Popular Languages

JavaScript / TypeScript
const regex = /^v?(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-]+)*))?$/;
const isValid = regex.test("v2.4.1-alpha.3+build.2026");
console.log(isValid); // true
Python (re)
import re

pattern = r"^v?(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-]+)*))?$"
match = re.match(pattern, "v2.4.1-alpha.3+build.2026")
print(bool(match)) # True
Go (regexp)
package main
import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile(`^v?(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-]+)*))?$`)
  fmt.Println(re.MatchString("v2.4.1-alpha.3+build.2026"))
}
Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

The regular expression pattern is /^v?(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-]+)*))?$/. Validates official Semantic Versioning 2.0.0 strings including pre-release and build metadata.