Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find First Vowel Using Regex (Javascript)

I am attempting to create a pig latin converter which splits the string at its first vowel, and switches the first substring with the second (e.g. dog -> ogd).

The following regex code is working for single vowel strings, however when attempting to translate a word with multiple vowels, it is splitting the string at the last vowel:

string.replace(/(\w+)([aeiou]\w+)/i, '$2$1')

Running this code on the word "meaning" results in "ingmean" (splitting on the 'i'), whereas I am expecting to return "eaningm" (splitting on the 'e')

Thanks!

like image 564
jgrune Avatar asked Jan 25 '17 16:01

jgrune


1 Answers

You need to add the lazy (?) operator:

string.replace(/(\w+?)([aeiou]\w+)/i, '$2$1')
like image 187
GilZ Avatar answered Sep 24 '22 02:09

GilZ