- 第一种:使用字符串的replace方法
let str = 'a nice girl';
let upperStr = str.replace(/\s+([^\s]*)/g,function($0,$1){
return $1.substring(0,1).toUpperCase()+$1.substring(1);
})
console.log(upperStr()) //aNiceGirl
复制代码
- 第二种:先将字符串转成数组,遍历数组转换,再拼成字符串
let a = 'hello world' ;
var b=a.split(' ').map(item=>{
return item[0].toUpperCase()+item.substr(1,item.length)
}).join('')
console.log(b)//helloWrold
复制代码