Reputation: 403
I'm trying with the following Regex
:
/^[aeiou]\..*[aeiou]$/
But it's not working, I tested "abcda" and didn't match.
Upvotes: 24
Views: 64708
Reputation: 3
I also have a problem like you, you can try this regex code. it work for my case
/^([aiueo]).*\1$/
Upvotes: 0
Reputation: 1
let endsWithVowel = function(str) { let regex = /.*[aeiouAEIOU]$/ return regex.test(str) }
let endsWithVowel = function(str) {
let regex = /.*[aeiouAEIOU]$/
return regex.test(str)
}
Upvotes: 0
Reputation: 151
/^(a|e|i|o|u).*\1$/
This works for me.
Explanation:
When you put ^
character, then you indicate that you are verifying the string for the start. The parentheses are group separators.
So, when putting (a|b|c)
you are telling that the first group to be tested will match with a
, b
, or c
. In the example, it marks as group (a|e|i|o|u)
. That means the string must start with any of those characters.
The |
character acts as a boolean OR comparator.
The .
indicates that any character except new line will be evaluated.
The *
indicates that we are expecting 0 or more characters that match.
The \1
acts as a reference from the result of the first group, in this case (a|e|i|o|u)
.
The $
character indicates the end of the string or the end of the line if we decide to go with multiline flag (/m
).
Upvotes: 14
Reputation: 1418
Okay if someone is looking for regex "end and start with same vowel" this is the code.
/^([a|e|i|o|u]).*\1$/
Upvotes: 0
Reputation: 1121
Actually none of the existing answers was covering the corner case of having single vowel.
/^[aeiou]$|^[aeiou].*[aeiou]$/
Breakdown explanation:
^[aeiou]$
^
- start of a string[aeiou]
- exactly one vowel.$
- end of a string.|
- OR:^[aeiou].*[aeiou]$
^
- start of a string[aeiou]
- exactly one vowel..*
any - zero or more[aeiou]
- exactly one vowel$
- end of a string.e.g match aba
, but not match abe
. We can backtrack to the first vowel:
/^[aeiou]$|^([aeiou]).*\1$/
Upvotes: 4
Reputation: 574
The regular expression for words beginning and ending with the same vowel is:
var re = /(\ba(\w+)a\b|\be(\w+)e\b|\bi(\w+)i\b|\bo(\w+)o\b|\bu(\w+)u\b)/g;
While the regular expression for words beginning and ending with the any vowel is:
var re = /\b[aeiou]\w+[aeiou]\b/gi;
Upvotes: 2
Reputation: 391
you can try this.
var re = /^([a,e,i,o,u]).*\1$/;
*\1 backreference and $ is used for the end.
Upvotes: 5
Reputation: 113
\b[aeiou](\w*[aeiou])?\b
\b
stands for word boundary (in this case, beginning and ending of word). The optional grouping ()?
is there to match single vowel words (very important for languages like portuguese with words like o
, a
and e
or even the english word I
.).
\b(?<vowel>[aeiou])(\w*\k<vowel>)?\b
Don't try to replace <vowel>
for some value. It´s there just for capturing the matched vowel.
Upvotes: 10
Reputation: 161
Regex for word start and end with vowel.
^[aeiou].*[aeiou]$
Regex for word start and end with same vowel.
^[a].*[a]$|^[e].*[e]$|^[i].*[i]$|^[o].*[o]$|^[u].*[u]$
Upvotes: 14
Reputation: 350725
It should be just:
/^[aeiou].*[aeiou]$/
The extra \.
you had would require the second character to be a literal dot, like in: "a.hello". But since your test case "abcda" does not include such a dot, it did not match.
Note that if you want to match upper case vowels as well, you could add the i
modifier, as follows:
/^[aeiou].*[aeiou]$/i
If your intention is to only match strings where the ending vowel is the same as the vowel at the start, then use a back-reference \1
like this:
/^([aeiou]).*\1$/i
Upvotes: 61
Reputation: 966
I think this regex suits you best:
/^a.*a$|^e.*e$|^i.*i$|^o.*o$|^u.*u$/
Upvotes: 2