Regex Not Capturing Complex Version Strings with Optional Trailing Metadata in JavaScript
I'm having trouble with I'm testing a new approach and I'm deploying to production and I'm sure I'm missing something obvious here, but I'm working on a JavaScript project where I need to extract version strings from a series of text inputs. The version strings can be formatted like `1.0.0`, `2.1.0-alpha`, or `3.2.1-beta.2`. The scenario arises when I try to capture the version strings that may or may not have trailing metadata (the part after a hyphen). I've attempted to use the following regex pattern: ```javascript const regex = /\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?/g; ``` When I test it against a string like this: ```javascript const str = 'The current versions are 1.0.0, 2.1.0-alpha, and 3.2.1-beta.2. Check them out!'; const matches = str.match(regex); console.log(matches); ``` I expect to get an array with all three version strings, but instead, I only get: ``` [ '1.0.0' ] ``` It seems that my regex fails to capture the versions with trailing metadata. I suspect the scenario lies in how the regex handles optional groups or maybe the global flag is causing problems. I've tried placing the `-` inside the capturing group, but that didn't help either. Can anyone provide insight on how to modify the regex to ensure it captures all version formats correctly? Is there a better way to handle this in JavaScript? I'm using Node.js version 14.17.0 for reference. Any help would be greatly appreciated! Am I approaching this the right way? I'm working with Javascript in a Docker container on Linux. What am I doing wrong?