Find the first vowel using regular expressions (Javascript)

I am trying to create a Latin pig converter that breaks a string into the first vowel and switches the first substring to the second (e.g. dog β†’ ogd).

The following regular expression code works for single vowel strings, however, when trying to translate a word with multiple vowels, it breaks the string into the last vowel:

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

Running this code in the word "value" results in "ingmean" (splitting on "i"), while I expect to return "eaningm" (splitting on "e")

Thanks!

+5
source share
2 answers

You need to add a lazy ( ? ) Operator:

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

That should do the trick

 /([^aeiou]+)([aeiou])([a-zA-Z]+)/ 

And use

$ 2 $ 3 $ 1

+5
source

Source: https://habr.com/ru/post/1263342/


All Articles