So, I am looking for a Regex that is able to match with every maximal non-empty substring of consonants followed by a maximal non-empty substring of vowels in a String
e.g. In the following strings, you can see all expected matches:
"zcdbadaerfe" = {"zcdba", "dae", "rfe"}
"foubsyudba" = {"fou", "bsyu", "dba"}
I am very close! This is the regex I have managed to come up with so far:
([^aeiou].*?[aeiou])
It returns the expected matches except for it only returns the first of any repeating lengths of vowels, for example:
String: "cccaaabbee"
Expected Matches: {"cccaaa", "bbee"}
Actual Matches: {"ccca", "bbe"}
I want to figure out how I can include the last found vowel character that comes before (a) a constant or (b) the end of the string.
Thanks! :-)
CodePudding user response:
Your pattern is slightly off. I suggest using this version:
[b-df-hj-np-tv-z] [aeiou]
This pattern says to match:
[b-df-hj-np-tv-z]
a lowercase non vowel, one or more times[aeiou]
followed by a lowercase vowel, one or more times
Here is a working demo.
CodePudding user response:
const rgx = /[^aeiou] [aeiou] (?=[^aeiou])|.*[aeiou](?=\b)/g;
Segment | Description |
---|---|
[^aeiou] |
one or more of anything BUT vowels |
[aeiou] |
one or more vowels |
(?=[^aeiou]) |
will be a match if it is followed by anything BUT a vowel |
| |
OR |
.*[aeiou](?=\b) |
zero or more of any character followed by a vowel and it needs to be followed by a non-word |
function lastVowel(str) {
const rgx = /[^aeiou] [aeiou] (?=[^aeiou])|.*[aeiou](?=\b)/g;
return [...str.matchAll(rgx)].flat();
}
const str1 = "cccaaabbee";
const str2 = "zcdbadaerfe";
const str3 = "foubsyudba";
console.log(lastVowel(str1));
console.log(lastVowel(str2));
console.log(lastVowel(str3));