Regex to Match Semantic Versioning

Validate semantic versioning (SemVer) strings with this regex pattern. Matches versions like 1.2.3, 1.0.0-beta.1, or 2.1.0+build.123. Free.

Regular Expression

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

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
(?:Start of non-capturing group
0Matches the literal character '0'
|Alternation — matches the expression before OR after the pipe
[1-9]Character class — matches any one of: 1-9
\dMatches any digit (0-9)
*Matches the preceding element zero or more times (greedy)
)End of group
\.Matches a literal dot
(?:Start of non-capturing group
0Matches the literal character '0'
|Alternation — matches the expression before OR after the pipe
[1-9]Character class — matches any one of: 1-9
\dMatches any digit (0-9)
*Matches the preceding element zero or more times (greedy)
)End of group
\.Matches a literal dot
(?:Start of non-capturing group
0Matches the literal character '0'
|Alternation — matches the expression before OR after the pipe
[1-9]Character class — matches any one of: 1-9
\dMatches any digit (0-9)
*Matches the preceding element zero or more times (greedy)
)End of group
(?:Start of non-capturing group
-Matches the literal character '-'
[\da-zA-Z-]Character class — matches any one of: \da-zA-Z-
+Matches the preceding element one or more times (greedy)
(?:Start of non-capturing group
\.Matches a literal dot
[\da-zA-Z-]Character class — matches any one of: \da-zA-Z-
+Matches the preceding element one or more times (greedy)
)End of group
*Matches the preceding element zero or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
(?:Start of non-capturing group
\+Matches a literal plus sign
[\da-zA-Z-]Character class — matches any one of: \da-zA-Z-
+Matches the preceding element one or more times (greedy)
(?:Start of non-capturing group
\.Matches a literal dot
[\da-zA-Z-]Character class — matches any one of: \da-zA-Z-
+Matches the preceding element one or more times (greedy)
)End of group
*Matches the preceding element zero or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates version strings following the Semantic Versioning (SemVer) specification. Here is the token-by-token breakdown:

^ — Anchors the match at the start.

(?:0|[1-9]\d*) — Matches the major version number. Either a single 0 or a non-zero digit followed by any number of digits. This prevents leading zeros (e.g., 01 is invalid).

. — Matches a literal dot separator.

(?:0|[1-9]\d*) — Matches the minor version number with the same no-leading-zeros rule.

. — Another dot separator.

(?:0|[1-9]\d*) — Matches the patch version number.

(?:-[\da-zA-Z-]+(?:.[\da-zA-Z-]+)*)? — An optional group for the pre-release identifier. It starts with a hyphen, followed by one or more alphanumeric characters or hyphens. Additional dot-separated identifiers are allowed (e.g., -beta.1 or -rc.1.2).

(?:+[\da-zA-Z-]+(?:.[\da-zA-Z-]+)*)? — An optional group for build metadata. It starts with a plus sign, followed by dot-separated alphanumeric identifiers. Build metadata is ignored for version precedence.

$ — Anchors the match at the end.

This pattern matches versions like 1.0.0, 2.1.3, 0.0.1-alpha, 1.2.3-beta.1, and 3.0.0+build.123. SemVer is the standard versioning scheme for software packages, used by npm, Cargo, and most package managers.

Example Test Strings

InputExpected
1.2.3Match
0.0.1-alphaMatch
1.0.0-beta.1+build.123Match
1.2No Match
01.2.3No Match
10.20.30Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 123 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →