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
JS 驼峰和下划线相互转换
最新推荐文章于 2024-09-02 15:52:41 发布