如:输入字符串a_bc_de
,返回字符串aBcDe
Ada
1 2 3 4 5
| function format (num) { return num.replace(/_(\w)/g, function(all, letter){ return letter.toUpperCase(); }); }
|
Beck
1 2 3 4 5 6 7
| function underscore2upper(str) { let words = str.split('_'); let newWords = words.map((element, index) => { return index === 0 ? element : element[0].toUpperCase() + element.substring(1, element.length) }); return newWords.join(''); }
|
Helen
1 2 3 4 5 6 7 8 9
|
function camelCase(str) { let outStr = str.replace(/_([a-zA-Z])/g, function (regValue, groupValue) { return groupValue.toUpperCase(); }); return outStr.replace(/_/g, ''); } console.log(camelCase('_____a_bc_de__________w___'));
|
Irene
1 2 3
| function test(item){ return item.replace(/_([a-zA-Z])/g, (a, b) => b.toUpperCase()) }
|
Mia
1 2 3 4 5 6 7 8 9 10 11
| function f2(str){ return str.split("_") .map(function(item, i){ if(i>0){ return item.replace(item[0],item[0].toUpperCase()); }else{ return item; } }).join(""); } f2("a_bc_de");
|
Jack
1 2 3 4 5 6 7
| function toString(foo){ var arr = foo.split('_'); for(let i=1; i<arr.length; i++){ arr[i]=arr[i].charAt(0).toUpperCase()+arr[i].substr(1); } return arr.join('') }
|