EN
TypeScript - convert camelCase to Sentence Case
0 points
In this article, we would like to show you how to convert camelCase
to Sentence Case
in TypeScript.
camelCase
to sentence case
conversion concept example:
xxxxxxxxxx
1
mySampleText -> My Sample Text
In the example below, we have created a function toSentenceCase()
that converts text from the camelCase convention to the sentence case convention. If the function gets no value, it returns an empty string.
xxxxxxxxxx
1
const toSentenceCase = (camelCase: string): string => {
2
if (camelCase) {
3
const result = camelCase.replace(/([A-Z])/g, ' $1');
4
return result[0].toUpperCase() + result.substring(1).toLowerCase();
5
}
6
return '';
7
};
8
9
10
// Usage example:
11
12
console.log(toSentenceCase('mySampleText')); // My sample text
13
console.log(toSentenceCase('anotherText')); // Another text
Output:
xxxxxxxxxx
1
My sample text
2
Another text