분류
javascript
Javascript 30초 Snippet - String : toTitleCase
본문
문자열을 title case로 변환합니다.
https://github.com/30-seconds/30-seconds-of-code#bytesize
정규 표현식을 사용하여 문자열을 단어로 나누고 각 단어의 첫 글자를 대문자로 묶고 공백을 추가하십시오.
const toTitleCase = 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.charAt(0).toUpperCase() + x.slice(1)) .join(' ');
ex)
toTitleCase('some_database_field_name'); // 'Some Database Field Name' toTitleCase('Some label that needs to be title-cased'); // 'Some Label That Needs To Be Title Cased' toTitleCase('some-package-name'); // 'Some Package Name' toTitleCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'Some Mixed String With Spaces Underscores And Hyphens'
- 이전글Javascript 30초 Snippet - String : truncateString 19.11.25
- 다음글Javascript 30초 Snippet - String : toSnakeCase 19.11.25