[英]Regex: Last Occurrence of a Repeating Character
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所以,我正在寻找一个正则表达式,它能够匹配每个最大非空 substring 个辅音,然后是一个字符串中最大非空 substring 个元音字母
eg 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.我想弄清楚如何在 (a) 常量或 (b) 字符串结尾之前包含最后找到的元音字符。
Thanks: :-)谢谢: :-)
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 [b-df-hj-np-tv-z]+
一个小写非元音字母,一次或多次[aeiou]+
followed by a lowercase vowel, one or more times [aeiou]+
后跟一个小写元音,一次或多次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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.