Converting Odd and Even-indexed characters in a string to uppercase/lowercase in Javascript?

后端 未结 4 729
眼角桃花
眼角桃花 2021-01-26 11:12

I need to make a function that reads a string input and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase.

function          


        
4条回答
  •  -上瘾入骨i
    2021-01-26 11:21

    RegExp alternative that handles space between characters :

    const alternativeCase = s => s.replace(/(\S\s*)(\S?)/g, (m, a, b) => a.toUpperCase() + b.toLowerCase());
    
    console.log( alternativeCase('alternative Case') )

提交回复
热议问题