我可以使用带条件的正则表达式吗?在Javascript中,如何为以辅音开头的单词和以元音开头的单词设置条件

我可以使用带条件的正则表达式吗?在Javascript中,如何为以辅音开头的单词和以元音开头的单词设置条件,javascript,Javascript,我想为以元音开头的单词和以辅音开头的单词设置条件: -如果一个单词以辅音开头,则取第一个辅音或辅音串,将其移到单词末尾,并在其上添加“ay” 如果一个单词以元音开头,只需在末尾加上“way” 函数翻译拉丁语(str){ 设regexConsonants=/([^aeiou]*)([aeiou]*)(.*)([aeiou]*)([^aeiou]*)(.+)/i 让replacedconsonstr=str.replace(regexconsants,“$2$3$1ay”); 让replaced

我想为以元音开头的单词和以辅音开头的单词设置条件:

-如果一个单词以辅音开头,则取第一个辅音或辅音串,将其移到单词末尾,并在其上添加“ay”

  • 如果一个单词以元音开头,只需在末尾加上“way”
函数翻译拉丁语(str){
设regexConsonants=/([^aeiou]*)([aeiou]*)(.*)([aeiou]*)([^aeiou]*)(.+)/i
让replacedconsonstr=str.replace(regexconsants,“$2$3$1ay”);
让replacedowelsstr=str.replace(/(.+)/i,“$1way”)
//console.log(replacedConsonantStr)
例如(设i=0;这是一个副本:在那篇帖子上读我的答案:
function translatePigLatin(str) {

  let regexConsonants=/(.[^aeiou]*)([aeiou]*)(.*)|([aeiou]*)(.[^aeiou]*)(.+)/i

  let replacedConsonantStr=str.replace(regexConsonants, "$2$3$1ay");

  let replacedVowelsStr=str.replace(/(.+)/i,"$1way")
  //console.log(replacedConsonantStr)



for(let i=0;i<str.length;i++){

if(str[0]=="a"|str[0]=="e"|str[0]=="i"|str[0]=="o"|str[0]=="u"){
        return replacedConsonantStr;                


   }else{
          return replacedVowelsStr
   }
        console.log(replacedVowelsStr)
}


}

translatePigLatin("algorithm");

//let regex=/(.[^aeiou]*)([aeiou]*)(.*)/i;

 //let replacedStr=str.replace(regex, "$2$3$1ay");
function translatePigLatin(str) {

  let regexConsonants=/(.[^aeiou]*)([aeiou]*)(.*)|([aeiou]*)(.[^aeiou]*)(.+)/i

  let replacedConsonantStr=str.replace(regexConsonants, "$2$3$1ay");

  let replacedVowelsStr=str.replace(/(.+)/i,"$1way")
  //console.log(replacedConsonantStr)





if(str.startsWith('a')|str.startsWith('e')|str.startsWith('i')|str.startsWith('o')|str.startsWith('u')){

             return replacedVowelsStr;
}else{
              return replacedConsonantStr;
}

}

translatePigLatin("algorithm");

//let regex=/(.[^aeiou]*)([aeiou]*)(.*)/i;