Camel to Kebab Case
Camel to Kebab Case - Explanation
Problem Summary
Write a function that converts camelCase to kebab-case.
Go to Problem →Detailed Explanation
이 문제는 **정규표현식**과 **케이스 변환**을 학습합니다. 웹 개발에서 CSS 속성과 JavaScript 속성 간 변환에 자주 사용됩니다. **케이스 스타일** - camelCase: backgroundColor (JavaScript) - kebab-case: background-color (CSS) - snake_case: background_color (Python) **정규표현식 분석: /([A-Z])/g** - `[A-Z]`: 대문자 A-Z 중 하나 - `()`: 캡처 그룹 (매치된 문자를 저장) - `g`: 전역 플래그 (모든 매치 찾기) **replace의 $1** $1은 첫 번째 캡처 그룹의 내용을 참조합니다: - "W"를 찾으면 → "-W"로 교체 - "C"를 찾으면 → "-C"로 교체 **"helloWorld" 처리 과정** 1. replace(/([A-Z])/g, '-$1') → "hello-World" 2. toLowerCase() → "hello-world" **"backgroundColor" 처리** 1. replace → "background-Color" 2. toLowerCase → "background-color" **대문자가 없는 경우** "simple"에는 대문자가 없으므로 replace가 아무것도 바꾸지 않고, toLowerCase도 변화 없이 "simple"을 반환합니다.
Solution Code
function camelToKebab(str) {
return str.replace(/([A-Z])/g, '-$1').toLowerCase();
}Key Concepts from This Problem
Common Mistakes
Hints
Complexity Analysis
Time Complexity
O(n)
Grows linearly with input size
Space Complexity
O(n)
Uses memory proportional to input size