Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|[a-zA-Z-][a-zA-Z0-9-]*))(?:\\((?:0|[1-9]\d*|[a-zA-Z-][a-zA-Z0-9-]*))?)?)?$

This regex matches a semantic versioning string (major.minor.patch) with optional pre-release and build metadata. The pre-release and build parts must follow specific alphanumeric or hyphenated naming rules.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Starts matching from the beginning
(0|[1-9]\d*) Group 1: Matches either '0' or a non-zero digit followed by any number of digits (major version number) 0, 1, 123
\. Literal dot: separates version components .
(0|[1-9]\d*) Group 2: Matches either '0' or a non-zero digit followed by any number of digits (minor version number) 0, 1, 45
\. Literal dot: separates version components .
(0|[1-9]\d*) Group 3: Matches either '0' or a non-zero digit followed by any number of digits (patch version number) 0, 1, 99
(?: Non-capturing group start: groups optional pre-release and build metadata Starts optional group
- Literal hyphen: introduces pre-release version -
((?:0|[1-9]\d*|[a-zA-Z-][a-zA-Z0-9-]*)) Group 4: Matches pre-release identifier (digits or alphanumeric with hyphens, starting with a letter or hyphen) alpha, 1, beta-1
(?: Non-capturing group start: groups optional build metadata Starts optional build metadata group
\\( Escaped literal parenthesis: introduces build metadata \(
(?:0|[1-9]\d*|[a-zA-Z-][a-zA-Z0-9-]*) Non-capturing group: matches build metadata (digits or alphanumeric with hyphens, starting with a letter or hyphen) 2023, x86-64
) Literal parenthesis: closes build metadata group )
)? Quantifier: makes the entire build metadata group optional Optional build metadata
)? Quantifier: makes the entire pre-release and build metadata group optional Optional pre-release and build
$ Anchor: asserts position at end of the string Ends matching at the end

Matches

  • 1.2.3
  • 0.0.0
  • 10.20.30
  • 1.2.3-alpha
  • 1.2.3-beta-1
  • 1.2.3+2023
  • 1.2.3-alpha+2023
  • 1.2.3-1+build-2023

Does not match

  • .1.2.3
  • 1..2.3
  • 1.2.
  • 1.2.3-
  • 1.2.3-+build
  • 1.2.3-alpha(build)
  • 1.2.3-01
  • 1.2.3-@invalid

Common pitfalls

  • Forgetting that pre-release identifiers cannot start with a digit unless the entire identifier is numeric (e.g., '1' is valid but '01' is not).
  • Assuming the build metadata can contain spaces or special characters beyond hyphens and alphanumerics.
  • Not accounting for the strict ordering of version components (major.minor.patch-pre-release+build).
  • Overlooking that the entire pre-release and build metadata section is optional.

Performance notes

The regex is generally efficient due to the use of non-capturing groups and quantifiers. However, nested quantifiers (e.g., \d*) in alternations could lead to catastrophic backtracking in pathological cases. Anchoring (^ and $) ensures the entire string is validated, preventing partial matches.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why is the pre-release identifier allowed to start with a hyphen?

The regex allows pre-release identifiers to start with a hyphen to accommodate common versioning practices like '1.2.3--alpha' (double hyphen prefix). However, this is not standard in semantic versioning and may be unintended.

Can the build metadata contain dots or other separators?

No. The build metadata must be a single alphanumeric string with hyphens (e.g., '2023', 'x86-64'). Dots or other separators are not allowed.

Why is the patch version group (0|[1-9]\d*) repeated three times?

The patch version group is repeated to enforce the semantic versioning format of major.minor.patch, where each component follows the same numeric rules (allowing '0' or non-zero numbers without leading zeros).