一、下划线转驼峰
1、效果
2、示例
function underscoreToCamelCase(str) {
return str.replace(/(_\w)/g, function(match) {
return match[1].toUpperCase();
}).replace(/^_/, '');
}
// 示例
let snakeCaseStr = "hello_world";
let camelCaseStr = underscoreToCamelCase(snakeCaseStr);
console.log(camelCaseStr); // "helloWorld"
二、驼峰转下划线
1、效果
2、示例
function camelToUnderscore(str) {
return str.replace(/[A-Z]/g, function(match) {
return "_" + match.toLowerCase();
}).replace(/^_/, '');
}
// 使用示例
let camelCaseStr = "camelCaseString";
let underscoredStr = camelToUnderscore(camelCaseStr);
console.log(underscoredStr); // "camel_case_string"
欢迎扫描下方二维码关注VX公众号