CodexBloom - Programming Q&A Platform

Regex scenarios to Extract Version Numbers from Software Release Notes in JavaScript - Need guide with Variants

👀 Views: 82 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-08
regex javascript node.js JavaScript

I'm converting an old project and I'm refactoring my project and I'm trying to extract version numbers from a block of release notes in a JavaScript application, but my regex isn't capturing all the variations correctly. The release notes include lines like: ``` v1.2.3 - Released on 2023-01-15 v1.2.3-beta - Released on 2023-02-01 v2.0.0-rc.1 - Released on 2023-03-01 Version 2.0.0 - Major release 2023-05-10 ``` I wrote the following regex pattern to match the versions: ```javascript const regex = /v?(\d+)\.(\d+)\.(\d+)(?:-(\w+\.\d+)?)?/g; ``` However, I'm working with issues where the regex is not matching the `v1.2.3-beta` and `v2.0.0-rc.1` formats properly. When I run the code, the output is just an empty array: ```javascript const releaseNotes = ` v1.2.3 - Released on 2023-01-15 v1.2.3-beta - Released on 2023-02-01 v2.0.0-rc.1 - Released on 2023-03-01 Version 2.0.0 - Major release 2023-05-10 `; const matches = releaseNotes.match(regex); console.log(matches); ``` The console logs an empty array instead of the expected version numbers. I'm also not sure if my current regex will handle all possible tags like `beta` or `rc`. What adjustments can I make to my regex to ensure I capture all these versions correctly? I'm using Node.js v16, and I'd appreciate any insights or corrections to improve my regex pattern. Thanks! This is happening in both development and production on Windows 11. Any advice would be much appreciated. My development environment is macOS. Thanks for taking the time to read this! This is for a mobile app running on Debian.