js 驼峰命名和下划线互换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 下划线转换驼峰
function camelize(name) {
return name.replace(/\_(w)/g, function(all, letter){
return letter.toUpperCase();
});
}

// 驼峰转换下划线
function hyphenate(name) {
return name.replace(/([A-Z])/g,'_$1').toLowerCase();
}

// 测试
let a = 'a_b2_345_c2345';
console.log(camelize(a));

let b = 'aBdaNf';
console.log(hyphenate(b));