I have several strings with the following structure:
AAAA_BBBB_CCCC_1_15_17
AAAA_BBBB_1
AAAA_BBBB_15_17
I am trying to find a regex that captures the following groups:
GRUPO1: AAAA GRUPO2: BBBB_CCCC GRUPO3: 1_15_17
GRUPO1: AAAA GRUPO2: BBBB GRUPO3: 1
GRUPO1: AAAA GRUPO2: BBBB GRUPO3: 15_17
I have tried the following:
([a-zA-Z]+)_([a-zA-Z]+_?[a-zA-Z]+?)_(\d+_?)+
I'm having trouble with the third group, as it seems that the next one match
overwrites the previous one using the +
for group (\d+_?)+
.
Example
const regex = /([a-zA-Z]+)_([a-zA-Z]+_?[a-zA-Z]+?)_(\d+_?)+/
const string = 'AAAA_BBBB_CCCC_1_2'
const [fullMatch, ...groups] = string.match(regex)
console.log(groups)
In the example 1_2
I only get the 2
.
How can I capture this last full group?