0
点赞
收藏
分享

微信扫一扫

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

浮游图灵 2022-03-18 阅读 82
javascript

// 下划线转换成驼峰

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 条评论