js - 驼峰命名

时间:2023-12-23 19:29:37

1.

// 驼峰命名
console.log(hump('border-bottom-color'))
function hump( str) {
if (typeof str != 'string') return;
var arr = str.split("-");
var newStr = arr[0];
for(var i = 1; i < arr.length; i++) {
newStr += arr[i].charAt(0).toLocaleUpperCase() + arr[i].substring(1);
}
return newStr;
} console.log(humpReg("border-bottom-color-size"))
function humpReg(str) {
var re = /\-(\w)/g;
str = str.replace(re, function (s) {
return s.slice(1).toUpperCase();
})
return str;
}