CodexBloom - Programming Q&A Platform

Regex scenarios to Match Version Numbers in a Custom Configuration File in Node.js

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

I'm converting an old project and I'm prototyping a solution and I'm writing unit tests and I'm wondering if anyone has experience with I'm optimizing some code but I'm trying to extract version numbers from a custom configuration file that has entries formatted like `version: 1.2.3` or `version: 2.0.0-beta`... However, my regex seems to be failing to match the versions correctly, especially when it comes to the optional pre-release tags. I'm currently using the following regex pattern: ```javascript const regex = /version:\s*(\d+\.\d+\.\d+(-[\w.]+)?)/g; ``` When I test this on a string like `version: 1.2.3 version: 2.0.0-beta version: 2.0.0`, I only get the first match as output, and I need to figure out why the subsequent matches are not being captured. Here's the code snippet I have: ```javascript const configContent = `version: 1.2.3\nversion: 2.0.0-beta\nversion: 2.0.0`; const matches = configContent.match(regex); console.log(matches); ``` This logs `[`version: 1.2.3`],` but I expected to see all version entries captured. I've also tried using `matchAll`, but it produces the same result. I'm running Node.js version 14.17.0. What am I missing in the regex pattern? It seems like a simple scenario, but I need to pinpoint the question. Any insights would be appreciated! This is part of a larger CLI tool I'm building. My development environment is CentOS. Has anyone else encountered this? My team is using Javascript for this mobile app. Any ideas how to fix this? This issue appeared after updating to Javascript latest. Any examples would be super helpful. I've been using Javascript for about a year now. Thanks, I really appreciate it! I'm developing on Ubuntu 20.04 with Javascript. Any examples would be super helpful.