JS 驼峰和下划线相互转换

34 阅读1分钟
let str1 = "hello_world_name"
 
// 下划线转驼峰
let str2 = str1.replace(/\_(\w)/g,function (match,letter) {
  return letter.toUpperCase()
})
console.log("str2",str2); //str2 helloWorldName
let str3 = "helloWorldName"
// 驼峰转下划线
let str4 = str3.replace(/[A-Z]/g,function (match,letter) {
  return "_" + match.toLowerCase()
})
console.log("str4",str4);//str4 hello_world_name