JavaScript Regular Expression: Extract Multiple Matching Patterns from Text

I’m working on a JavaScript project where I need to parse a string that contains repeating patterns. Here’s what my data looks like:

Sample text here with some content >>(Item:HelloWorld###testdata###sampletext###2.5678###confirmed),(Item:HelloWorld###testdata###sampletext###2.5678###confirmed) continues…

The structure follows this format:

(category:value###value###value###number###status),(category:value###value###value###number###status) repeating…

I want to extract all these patterns and convert them into a clean JSON structure. Right now my regex only captures the first match instead of all occurrences.

Here’s my current approach:

var inputText = '(Item:HelloWorld###testdata###sampletext###2.5678###confirmed),(Item:HelloWorld###testdata###sampletext###2.5678###confirmed)';

var pattern1 = '.*?';
var pattern2 = '(?:[a-z][a-z]+)';
var pattern3 = '.*?';
var pattern4 = '((?:[a-z][a-z]+))';
var pattern5 = '.*?';
var pattern6 = '((?:[a-z][a-z]+))';
var pattern7 = '.*?';
var pattern8 = '((?:[a-z][a-z]+))';
var pattern9 = '.*?';
var pattern10 = '([+-]?\\d*\\.\\d+)(?![-+0-9\\.])';
var pattern11 = '.*?';
var pattern12 = '((?:[a-z][a-z]+))';

var regex = new RegExp(pattern1+pattern2+pattern3+pattern4+pattern5+pattern6+pattern7+pattern8+pattern9+pattern10+pattern11+pattern12, ['i']);
var result = regex.exec(inputText);

if (result != null) {
    var firstWord = result[1];
    var secondWord = result[2];
    var thirdWord = result[3];
    var numberValue = result[4];
    var statusWord = result[5];
    console.log('(' + firstWord + ')' + '(' + secondWord + ')' + '(' + thirdWord + ')' + '(' + numberValue + ')' + '(' + statusWord + ')');
}

How can I modify this to capture all matching patterns instead of just the first one?

Your regex is way too complex and only grabs the first match because you’re missing the global flag with exec(). exec() only returns one match at a time, and that massive regex pattern you built is making things harder than they need to be.

Ditch the concatenated regex and use matchAll() with something cleaner:

var inputText = '(Item:HelloWorld###testdata###sampletext###2.5678###confirmed),(Item:HelloWorld###testdata###sampletext###2.5678###confirmed)';

var pattern = /\(([^:]+):([^#]+)###([^#]+)###([^#]+)###([\d.]+)###([^)]+)\)/g;
var matches = [...inputText.matchAll(pattern)];

var results = matches.map(match => ({
    category: match[1],
    value1: match[2],
    value2: match[3], 
    value3: match[4],
    number: parseFloat(match[5]),
    status: match[6]
}));

console.log(JSON.stringify(results, null, 2));

Two main fixes: use the global flag ‘g’ and matchAll() to get all matches. This pattern actually captures your data structure instead of trying to match random lowercase words.

The problem is you’re using exec() but not looping through all matches. Switch to match() with the global flag - it’s way simpler than matchAll here. Also, your regex is way too complicated. Just use /\(([^:]+):([^#]+)###([^#]+)###([^#]+)###([\d.]+)###([^)]+)\)/g and it’ll capture everything in one shot instead of juggling separate pattern variables.

Hit this exact same issue last month parsing log files. The problem is exec() only returns one match per call - you’ve got to loop it to get everything. Here’s what worked for me:

var inputText = '(Item:HelloWorld###testdata###sampletext###2.5678###confirmed),(Item:HelloWorld###testdata###sampletext###2.5678###confirmed)';

var pattern = /\(([^:]+):([^#]+)###([^#]+)###([^#]+)###([\d.]+)###([^)]+)\)/g;
var allMatches = [];
var match;

while ((match = pattern.exec(inputText)) !== null) {
    allMatches.push({
        category: match[1],
        val1: match[2],
        val2: match[3],
        val3: match[4],
        number: parseFloat(match[5]),
        status: match[6]
    });
}

console.log(allMatches);

Just use the global flag and loop with exec(). Your original regex was way too complicated - this simpler pattern grabs the structure better and handles multiple matches properly.