Regex for Semantic Version
Validates semantic versioning (semver) strings with optional pre-release and build metadata.
Pattern
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/ Live Tester
Enter a string to test
Examples
✓ 1.2.3
✓ 0.0.1
✓ 1.0.0-beta.1
✗ 1.2
✗ v1.2.3
✗ 01.2.3
Pattern Breakdown
^(0|[1-9]\d*) — major version (no leading zeros)
\.(0|[1-9]\d*) — minor version
\.(0|[1-9]\d*) — patch version
(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)? — optional pre-release identifiers
(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$ — optional build metadata
Code Snippets
Javascript
const regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/;
regex.test("1.0.0-beta.1"); // true Python
import re
pattern = r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$"
bool(re.match(pattern, "1.0.0-beta.1")) # True Php
$pattern = '/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/';
preg_match($pattern, "1.0.0-beta.1"); // 1 Related Patterns
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv4 Address /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
Frequently Asked Questions
What is the regex for Semantic Version?▾
The regex pattern for Semantic Version is /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?(\+[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*)?$/. Validates semantic versioning (semver) strings with optional pre-release and build metadata.
How do I use the Semantic Version regex in JavaScript?▾
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Semantic Version pattern match?▾
It matches strings like 1.2.3; it rejects strings like 1.2. See the Examples section above for the full list of matching and non-matching cases.