简体   繁体   English

使用正则表达式找到第一个元音(Javascript)

[英]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 (eg dog -> ogd). 我正在尝试创建一个猪拉丁转换器,它将字符串分割为第一个元音,并将第一个子字符串切换为第二个子字符串(例如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') 在“意义”这个词上运行这个代码导致“ingmean”(在'i'上分裂),而我期望返回“eaningm”(在'e'上分裂)

Thanks! 谢谢!

This should do the trick 这应该可以解决问题

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

And use 并使用

$2$3$1 $ 2 $ 3 $ 1

你需要添加lazy( ? )运算符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM