nathanbweb
nathanbweb

Reputation: 717

regex for vowels at word boundaries

I'm trying to match all words that satisfy the condition of having a vowel occur at the beginning and ending of a word.

What I tried (in JS so I could fiddle it):

var t = 'are ice apple cat dog'
var u =  t.match(/(\b[aeiou]\w+[aeiou]\b)+/); 
alert (u);  // should match 'are,ice,apple'

Upvotes: 3

Views: 2559

Answers (2)

Wiseguy
Wiseguy

Reputation: 20873

Give it the g global flag so it will match all. You could drop the outer ( )+, too, as it won't gain you anything.

var u =  t.match(/\b[aeiou]\w+[aeiou]\b/g); 
                                        ^

http://jsfiddle.net/XTAtc/

Upvotes: 3

Jeffrey Gunderson
Jeffrey Gunderson

Reputation: 11

If you want to match all the words that satisfy a condition you need to at the 'g' modifier to your regexpression

http://www.w3schools.com/jsref/jsref_regexp_g.asp

var t = 'are ice apple cat dog'
var u =  t.match(/(\b[aeiou]\w+[aeiou]\b)+/g); 
alert (u);  // it now matches 'are,ice,apple'

Upvotes: 1

Related Questions