L.toUpperCase()); } "I'm a little tea pot".match(/(^|\s)\S/g)//["I", " a", " l", " t", " p"]"> L.toUpperCase()); } "I'm a little tea pot".match(/(^|\s)\S/g)//["I", " a", " l", " t", " p"]"> L.toUpperCase()); } "I'm a little tea pot".match(/(^|\s)\S/g)//["I", " a", " l", " t", " p"]">
//use array
function titleCase(str) {
let lowerCasewords = str.toLowerCase().split(' ')
let titleCaseWords = lowerCasewords.map(word=> {
return word.replace(word.charAt(0),word.charAt(0).toUpperCase())
})
return titleCaseWords.join(' ');
}
titleCase("I'm a little tea pot");
//use regex
function titleCase(str) {
//每匹配一次,就替换一次,每次替换都调用函数
return str.toLowerCase().replace(/(^|\\s)\\S/g, L => L.toUpperCase());
}
"I'm a little tea pot".match(/(^|\\s)\\S/g)//["I", " a", " l", " t", " p"]