Keep-a-Changelog Entry Header in JS
Match Keep-a-Changelog style version headers like `## [1.2.3] - 2024-01-15` or `## 2.0.0`.
Try it in the JS tester →Pattern
regexJS
^##\s+\[?([\w.\-]+)\]?(?:\s*-\s*(\d{4}-\d{2}-\d{2}))? (flags: gm)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^##\\s+\\[?([\\w.\\-]+)\\]?(?:\\s*-\\s*(\\d{4}-\\d{2}-\\d{2}))?", "gm");
const input = "## [1.2.3] - 2024-01-15\\n## [Unreleased]";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
^## matches the markdown H2 marker. \s+ requires whitespace. \[? optionally matches an opening bracket. ([\w.\-]+) captures the version (semver, calendar version, or label like `Unreleased`). \]? optionally matches the closing bracket. (?:\s*-\s*(\d{4}-\d{2}-\d{2}))? optionally captures an ISO date.
Examples
Input
## [1.2.3] - 2024-01-15\n## [Unreleased]Matches
## [1.2.3] - 2024-01-15## [Unreleased]
Input
## 2.0.0 - 2025-12-25Matches
## 2.0.0 - 2025-12-25
Input
# TitleNo match
—