📜

Camel to Kebab Case

Camel to Kebab Case - Explanation

보통 문자열 O(n) O(n)

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

solution.js
function camelToKebab(str) {
  return str.replace(/([A-Z])/g, '-$1').toLowerCase();
}

Key Concepts from This Problem

1. 정규표현식 캡처 그룹
2. replace 메서드
3. 케이스 변환
4. $1 참조

Common Mistakes

g 플래그 없이는 첫 번째 대문자만 변환됩니다
toLowerCase를 먼저 하면 대문자를 찾을 수 없습니다
첫 글자가 대문자면 앞에 불필요한 -가 붙을 수 있습니다

Hints

Hint 1: 정규표현식으로 대문자를 찾으세요.
Hint 2: 대문자 앞에 -를 추가하세요.

Complexity Analysis

Time Complexity

O(n)

Grows linearly with input size

Space Complexity

O(n)

Uses memory proportional to input size

Related Tags

#문자열 #정규표현식 #변환