JS 字符串驼峰和下划线互相转换

下划线转换驼峰

function toHump(name) {
    return name.replace(/\_(\w)/g, function(all, letter){
        return letter.toUpperCase();
    });
}
let a = 'a_b2_345_c2345';
console.log(toHump(a));

驼峰转换下划线

function toLine(name) {
  return name.replace(/([A-Z])/g,"_$1").toLowerCase();
}
let b = 'aBdaNf';
console.log(toLine(b));

你可能感兴趣的:(JS 字符串驼峰和下划线互相转换)