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(/\b(\w)(\w+)\b/ig,"$2$1ay");
}
  • other answer:
function pigIt(str){
  return str.split(' ').map(function(el){
    return el.slice(1) + el.slice(0,1) + 'ay';
  }).join(' ');
}
posted @ 2017-09-02 14:01  芒果夏夏  阅读(125)  评论(0编辑  收藏  举报