29 lines
1.0 KiB
Markdown
29 lines
1.0 KiB
Markdown
---
|
|
title: toTitleCase
|
|
tags: string,regexp,intermediate
|
|
firstSeen: 2018-10-19T04:49:34+03:00
|
|
lastUpdated: 2020-10-22T20:24:44+03:00
|
|
---
|
|
|
|
Converts a string to title case.
|
|
|
|
- Use `String.prototype.match()` to break the string into words using an appropriate regexp.
|
|
- Use `Array.prototype.map()`, `Array.prototype.slice()`, `Array.prototype.join()` and `String.prototype.toUpperCase()` to combine them, capitalizing the first letter of each word and adding a whitespace between them.
|
|
|
|
```js
|
|
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(' ');
|
|
```
|
|
|
|
```js
|
|
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'
|
|
```
|