Pig Latin 把一个英文单词的第一个辅音或辅音丛(consonant cluster)移到词尾,然后加上后缀 "ay"。
如果单词以元音开始,你只需要在词尾添加 "way" 就可以了。
这是一些对你有帮助的资源:
先将以前的解法拿来用了:
function translate(str) { var toCpar=str[0].toLowerCase(); var result=''; if(toCpar=='a'||toCpar=='e'||toCpar=='i'||toCpar=='o'||toCpar=='u'){ result=str.concat('way'); }else if(str[1]=='a'||str[1]=='e'||str[1]=='i'||str[1]=='o'||str[1]=='u'){ result=str.slice(1).concat(str.split('').splice(0,1)+'ay'); }else{ result=str.slice(2).concat(str.split('').splice(0,2).join('')+'ay'); } return result; }
正确解法应该是这样的:
function translatePigLatin(str) { var reg = /[aeiou]/gi; var result = ''; var idx; if(str[0].match(reg)){ result = str+'way'; }else if(str.match(reg)===null){ result = str + 'ay'; }else{ idx = str.indexOf(str.match(reg)[0]); result = str.substring(idx)+str.substring(0,idx)+'ay'; } return result; }