12.字符串转换为驼峰命名法
完成方法/函数,使得其短划线/下划线分隔的单词转换成骆驼套管。输出中的第一个词应该如果原词是大写只大写。
例子:
// returns "theStealthWarrior"
toCamelCase("the-stealth-warrior")
// returns "TheStealthWarrior"
toCamelCase("The_Stealth_Warrior")
function toCamelCase(str){
str=str.split(/[-_]/);
console.log(str)
return str.map(function(x,i){
if(i>0){
return x.charAt(0).toUpperCase()+x.slice(1)
}else{
return x
}
}).join("")
}
主要的难点在于使用正则,这是我现在还不会的内容,需要加强,以下为测试的别人写的简介答案
return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
话说看不懂的说,明白的同学可以解释一下啊?