RegexWars
Tier 3 · Warlockcapture

Semver, Prerelease And All

Capture the major, minor and patch numbers of a version, plus its prerelease tag — or nothing in group 4 when there is no prerelease.

An optional group that does not take part in the match captures nothing at all, which is exactly what group 4 should do for a plain release. Prerelease tags are not always one word — the spec calls them dot-separated identifiers, and an identifier may contain hyphens, so the hyphen that introduces the tag is not the only one you will meet.

Your pattern0 chars · par 34
//
Flags
Start typing — tests run as you go.0 of 6 visible tests passing
Test cases
  • 1.2.3→ ["1", "2", "3", null]
  • v0.1.0→ ["0", "1", "0", null]
  • 2.0.0-rc.1→ ["2", "0", "0", "rc.1"]
  • 10.4.11-beta→ ["10", "4", "11", "beta"]
  • not a versionmust not match
  • 1.0.0-alpha-1→ ["1", "0", "0", "alpha-1"]

    prerelease identifiers may contain hyphens

+ 4 hidden tests, checked when you submit. They are what stops a pattern that only fits the examples above.