Pig Latin
字符串开头是非元音,可能有N 个非元音,把非元音移至字符串尾部,并再在尾部加上ay
字符串开头是元音,直接在尾部添加way
function translatePigLatin(str) {
if(/^[aeiou]/.test(str)) {
return str+ "way"
}else {
let vowelIndex= str.search(/[aeiou]/)
if(vowelIndex=== -1)//没有元音字母
return str+ "ay"
return str.slice(vowelIndex)+ str.slice(0, vowelIndex)+ "ay"
}
}
translatePigLatin("rhythm");
测试数据:
california paragraphs glove algorithm eight schwartz rhythm
//别人家的孩子 /(ㄒoㄒ)/~~
function translatePigLatin(str) {
return str
.replace(/^[aeiou]\\w*/, "$&way")//元音开头
.replace(/(^[^aeiou]+)(\\w*)/, "$2$1ay");//开头非元音字符和其余字符交换位置,好骚啊
}
translatePigLatin("consonant");