Write a regex that validates a semver string like 1.2.3, and explain each part.
Here is a strict regex for plain MAJOR.MINOR.PATCH versions:
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$
Breaking it down:
^ … $ — anchors: the whole string must match, no extra characters.
(0|[1-9]\d*) — a number with no leading zeros: either exactly 0, or a digit 1–9 followed by any digits.
\. — a literal dot separating the three groups.
If you also need pre-release tags like 1.2.3-beta.1, say the word and I’ll extend it.
Yes, extend it to support pre-release tags.
Acme AI can make mistakes. Check important information.