JackIsJack
JackIsJack

Reputation: 88

Regex non exclusive group

Do you how can I get this result with Regex? In literal words, I want each groups of successives vowels with the maximum of consonants surrounding them (backward or foreward).

Example :

input : alliibaba 

outputs :
[all]
[lliib]
[bab]
[ba]

I tried :

[bcdfghjklmnpqrstvwxyz]*[aeiou]+[bcdfghjklmnpqrstvwxyz]*

but it returns distincts groups, so I don't know if it's possible with Regex...

Thanks you for any help.

Upvotes: 2

Views: 77

Answers (1)

blhsing
blhsing

Reputation: 106445

You can put the part of the regex that you want to be made non-exclusive in a lookaround pattern so that it leaves that portion of the search buffer for the next match. Since your rule appears to be that vowels do not overlap between matches while the surrounding consonants can, you can group the consonants after vowels in a lookahead pattern, while putting vowels and any preceding consonants in another capture group, and then concatenate the 2 matching groups into a string for output:

var re = /([bcdfghjklmnpqrstvwxyz]*[aeiou]+)(?=([bcdfghjklmnpqrstvwxyz]*))/g;
var s = 'alliibaba';
var m;

do {
    m = re.exec(s);
    if (m)
        console.log(m[1] + m[2])
} while (m);

Upvotes: 5

Related Questions