最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Find First Vowel Using Regex (Javascript) - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question edited Jan 25, 2017 at 16:15 jgrune asked Jan 25, 2017 at 16:13 jgrunejgrune 1031 silver badge7 bronze badges 2
  • You might want to be specific about the exact output you are trying to get – musefan Commented Jan 25, 2017 at 16:15
  • @musefan I am hoping to return "eaningm" – jgrune Commented Jan 25, 2017 at 16:17
Add a ment  | 

2 Answers 2

Reset to default 5

You need to add the lazy (?) operator:

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

This should do the trick

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

And use

$2$3$1

发布评论

评论列表(0)

  1. 暂无评论