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

阅读 86

2022-03-18

// 下划线转换成驼峰

function toHump(name) {
    return name.replace(/\_(\w)/g, function(all, letter){
        return letter.toUpperCase();
    });
}
toHump('hello_js_go') //helloJsGo

// 驼峰转换下划线

function toLine(name) {
  return name.replace(/([A-Z])/g,"_$1").toLowerCase();
}
toLine('aBcdEfg') //a_bcd_efg
 

相关推荐

精彩评论(0)

0 0 举报