美文网首页
js字符串驼峰和下划线互相转换

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

作者: Coldhands | 来源:发表于2023-07-19 15:01 被阅读0次
// 下划线转换成驼峰
function toHump(name) {
    return name.replace(/\_(\w)/g, function(all, letter){
        return letter.toUpperCase();
    });
}
toHump('a_b_c') // aBC


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

相关文章

网友评论

      本文标题:js字符串驼峰和下划线互相转换

      本文链接:https://www.haomeiwen.com/subject/fbzoudtx.html