Regex in JavaScript/Basic semver parser

Extract version parts from a semantic version string.

Section: JavaScript Recipes

Basic semver parser

javascript
javascript
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/
Explanation

Handy for tooling and validation.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing JavaScript Recipes
Basic email shape
Check a simple email-like structure.
OpenIn sheetjavascriptsame section
Slug validator
Match lowercase slugs with hyphens.
OpenIn sheetjavascriptsame section
Key=value extractor
Extract query-like key-value pairs.
OpenIn sheetjavascriptsame section
Collapse internal whitespace
Normalize repeated whitespace down to single spaces.
OpenIn sheetjavascriptsame section
Redact bearer token
Mask bearer tokens in logs.
OpenIn sheetjavascriptsame section
Find hex colors
Extract CSS-like hex color literals.
OpenIn sheetjavascriptsame section