Paras Singh
Paras Singh

Reputation: 403

Regex to match beginning and end of a word with a vowel

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

Answers (14)

wahyouka
wahyouka

Reputation: 3

I also have a problem like you, you can try this regex code. it work for my case

/^([aiueo]).*\1$/

Upvotes: 0

Eyasu Berg
Eyasu Berg

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

kaju
kaju

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

Kunal Bhatt
Kunal Bhatt

Reputation: 51

or you can just write like this :-

/^([aeiou]).+\1$/;

Upvotes: 1

Jitesh Dhamaniya
Jitesh Dhamaniya

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$/

Demo

Upvotes: 0

Nas
Nas

Reputation: 1121

Actually none of the existing answers was covering the corner case of having single vowel.

Begins and ends with any 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.

Begins and ends with one and the same vowel

e.g match aba, but not match abe. We can backtrack to the first vowel:

/^[aeiou]$|^([aeiou]).*\1$/

Upvotes: 4

Rishu Ranjan
Rishu Ranjan

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

Sundaram Seth
Sundaram Seth

Reputation: 391

you can try this.

var re = /^([a,e,i,o,u]).*\1$/;

*\1 backreference and $ is used for the end.

Upvotes: 5

Krishna Chaitanya
Krishna Chaitanya

Reputation: 11

This will work:

/^([aeiou]).+\1$/

Upvotes: 0

omboido
omboido

Reputation: 113

Words beginning and ending with vowels

\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.).

Words beginning and ending with the same vowel

\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

Baljeet Verma
Baljeet Verma

Reputation: 1

Try this: /[aeiou]+[a-z]+[aeiou]+/i

Upvotes: -2

Navin
Navin

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

trincot
trincot

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

Nick
Nick

Reputation: 966

I think this regex suits you best:

/^a.*a$|^e.*e$|^i.*i$|^o.*o$|^u.*u$/

Upvotes: 2

Related Questions