JavaScript 'Pig latin is cool'==>'igPay atinlay siay oolcay'

Description:

Move the first letter of each word to the end of it, then add 'ay' to the end of the word.

console.log(pigIt('Pig latin is cool')); // igPay atinlay siay oolcay
  • my answer:使用正则
function pigIt(str){
  return str.replace(/(w)(w*)(s|$)/g, "$2$1ay$3")
}
function pigIt(str){
  return str.replace(/(w)(w+)/ig,"$2$1ay");
}
  • other answer:
function pigIt(str){
  return str.split(' ').map(function(el){
    return el.slice(1) + el.slice(0,1) + 'ay';
  }).join(' ');
}
原文地址:https://www.cnblogs.com/kid2333/p/7466589.html