简体   繁体   English

正则表达式:重复字符的最后一次出现

[英]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]+后跟一个小写元音,一次或多次

Here is a working demo .这是一个工作演示

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.

相关问题 正则表达式:字符之前的最后一次出现 - Regex: last occurrence of character before 正则表达式匹配重复模式但不是最后一次出现 - Regex match repeating patterns but not last occurrence 正则表达式:一个字符的最后一次出现和另一个字符的第一次出现之间的匹配 - Regex: match between last occurrence of a character and first occurrence of another character 创建一个正则表达式来替换字符串中最后一次出现的字符 - Create a regex to replace the last occurrence of a character in a string JavaScript RegEx-替换字符的第一个和最后一个出现 - JavaScript RegEx - replace first and last occurrence of a character 正则表达式删除除单个字符的最后一次出现以外的所有字符 - Regex to remove all but the last occurrence of a single character 正则表达式删除除第一次出现和最后一次出现的特殊字符 - regex remove special character except first occurrence and last occurrence 在最后一次出现特定字符串REGEX时拆分字符串 - Split a string at last occurrence of a particular character string REGEX 如果在最后一次出现特定字符后包含某些字符串,则正则表达式匹配 - Regex match if certain string is contained after last occurrence of specific character 正则表达式提取字符的第一次出现和最后一次出现之间的字符串 - Regex to extract string between the first and last occurrence of a character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM