完整的HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<script>
//驼峰命名的变量转成短横线命名
function camelToDash(str){
return str.replace(/[A-Z]/g, function(item) {
console.log('item='+item) //user-name
return '-'+item.toLowerCase()
})
}
console.log( camelToDash( 'userNameAgeEmail' ) ) //user-name
//短横线命名的变量转成驼峰命名
function dashToCamel(str) {
return str.replace(/-([a-z])/g,function(keb,item){
return item.toUpperCase();
} )
}
console.log( dashToCamel( 'user-name-age-email' ) ); //userName
</script>
</body>
</html>