[英]Matching words starting and ending with same vowel letter
I am trying to come up with a regex to match words starting and ending with same vowel.我试图想出一个正则表达式来匹配以相同元音开头和结尾的单词。 My question is, is this an elegant all-encompassing solution or am i missing something?我的问题是,这是一个优雅的包罗万象的解决方案还是我遗漏了什么? So far this is what I have come up after a quick brainstroming.到目前为止,这是我经过快速头脑风暴后得出的结论。 My preferred environment is javascript/python without special libraries etc. Thanks for helpful suggestions.我的首选环境是没有特殊库等的 javascript/python。感谢您提供有用的建议。
re = /(^[aeiou])\\w+\\1/i;
1) re = /(^[aeiou])\\w+\\1/i;
re = /(^[aeiou])[a-zA-Z]+\\1/i;
2) re = /(^[aeiou])[a-zA-Z]+\\1/i;
console.log(re.test("abcda")); //true
console.log(re.test("abcdo")); //false
Words can be matched using a word boundary \\b
:可以使用单词边界\\b
匹配单词:
var re = /\b([aeiou])[a-z]+\1\b/i;
The regex matches:正则表达式匹配:
\\b
- leading word boundary (because the pattern after it matches a word character) \\b
- 前导词边界(因为它匹配一个词字符后的模式)([aeiou])
- Group 1 capturing a vowel from the specified range ([aeiou])
- 第 1 组捕获指定范围内的元音[az]+
- one or more letters (both upper- and lowercase since the /i
modifier is used) [az]+
- 一个或多个字母(大写和小写,因为使用了/i
修饰符)\\1
- backreference to the vowel captured with the first group \\1
- 对第一组捕获的元音的反向引用\\b
- trailing word boundary \\b
- 尾随词边界
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.