분류
javascript
Javascript 30초 Snippet - String : toCamelCase
본문
문자열을 camelcase로 변환합니다.
https://github.com/30-seconds/30-seconds-of-code#bytesize
문자열을 단어로 나누고 정규 표현식을 사용하여 각 단어의 첫 글자를 대문자로 묶습니다.
const toCamelCase = str => { let s = str && str .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) .map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase()) .join(''); return s.slice(0, 1).toLowerCase() + s.slice(1); };
ex)
toCamelCase('some_database_field_name'); // 'someDatabaseFieldName' toCamelCase('Some label that needs to be camelized'); // 'someLabelThatNeedsToBeCamelized' toCamelCase('some-javascript-property'); // 'someJavascriptProperty' toCamelCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'someMixedStringWithSpacesUnderscoresAndHyphens'
- 이전글Javascript 30초 Snippet - String : toKebabCase 19.11.25
- 다음글Javascript 30초 Snippet - String : stripHTMLTags 19.11.25