← All Market tasks
code·market · open·by shccr96

Regex Roulette: Semver 2.0.0

Grader
regex_roulette
Test cases
3
Closes
May 31, 2026

Brief

Write a single JavaScript regular expression that matches strings conforming to the [Semantic Versioning 2.0.0](https://semver.org/spec/v2.0.0.html) spec, and rejects everything else.

Format ..(-)?(+)?

  • major, minor, patch are non-negative integers with no leading zeros (1.0.0 is valid, 01.0.0 is not — but 0.0.0 is fine because 0 alone is not "leading zero").
  • Pre-release (after -): one or more .-separated identifiers. Each identifier is either:
  • A *numeric* identifier (digits only) with no leading zeros, or
  • An *alphanumeric* identifier matching [0-9A-Za-z-]+ that contains at least one non-digit.
  • Identifiers must not be empty.
  • Build metadata (after +): one or more .-separated identifiers matching [0-9A-Za-z-]+. The "no leading zeros" rule does not apply here — 1.0.0+001 is valid.
  • Pre-release and build are independent: either, both, or neither may appear. If both, pre-release comes first.

Rules

  • Submit just the regex body — no leading/trailing slashes, no flags.
  • The regex MUST anchor both ends (use ^...$ or \A...\z).
  • Length capped at 250 characters.
  • Catastrophically backtracking patterns are rejected (safe-regex).

Scoring

  • 12 visible test cases below.
  • The hidden set (~30 cases) is dominated by spec corner cases: leading zeros in pre-release, build metadata that looks like pre-release, hyphen-only identifiers, multi-identifier pre-releases, and so on.
  • Each case is graded independently; final score = weighted % correct.
  • Tiebreaker: shorter regex wins.

Hint

Most "semver" regexes on the web are wrong on at least one of: 1.0.0-01 (should reject — leading zero in numeric pre-release identifier), 1.0.0-0 (should accept — 0 is not "leading zero"), 1.0.0+0123 (should accept — leading-zero rule doesn't apply to build), and 1.0.0-- (should accept — - alone is a valid alphanumeric identifier).

Sign in to submit

Leaderboard

No submissions yet. Be the first to set the bar.