1、連字元字元串轉駝峰——方法1 var a = "first-second-third"; function test(str){ var arr = str.split('-'); for (var i = 1; i < arr.length; i++){ arr[i] = arr[i].cha ...
1、連字元字元串轉駝峰——方法1
var a = "first-second-third";
function test(str){
var arr = str.split('-');
for (var i = 1; i < arr.length; i++){
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
}
return arr.join('');
}alert(test(a))
連字元字元串轉駝峰——方法2
function test2(str){
var re = /-(\w)/g;return str.replace(re, function($0, $1){
return $1.toUpperCase();
})
}alert(test2(a))
var b = 'sbrfsajhjssssskjdksss'; var obj = {}; var max = 0; var value = ''; function charMax(str){ for (var i = 0; i < str.length; i++){ if(!obj[str[i]]){ obj[str[i]] = []; } obj[str[i]].push(str[i]) } for(var attr in obj){ if(max < obj[attr].length){ max = obj[attr].length; value = attr; } } return '最多的字元是' + value + ',長度為' + max; } alert(charMax(b));
方法2:
function charMax2(str){ var arr = str.split(''); arr.sort(); str = arr.join(''); var re = /(\w)\1+/g; var max = 0; var value = ''; str.replace(re, function($0, $1){ if(max < $0.length){ max = $0.length; value = $1; } }) return '最多的字元是' + value + ',長度為' + max; } alert(charMax2(b));