JavaScript 之下划线转换驼峰和驼峰转换下划线
下划线转换驼峰
function toHump(lineStr) {
return lineStr.replace(/\_(\w)/g, function (all, letter) {
return letter.toUpperCase();
});
}
驼峰转换下划线
function toLine(humpStr) {
return humpStr.replace(/([A-Z])/g, "_$1").toLowerCase();
}
测试
let lineStr = 'hello_world';
console.log(toHump(lineStr));
let humpStr = 'helloWord';
console.log(toLine(humpStr));
结果
helloWorld
hello_word