EN
JavaScript - convert camelCase to Sentence Case
3 points
In this article, we would like to show you how to convert camelCase
to Sentence Case
in JavaScript.
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 => {
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
// Usage example:
10
11
console.log( toSentenceCase('mySampleText') ); // My sample text
12
console.log( toSentenceCase('anotherText') ); // Another text