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:
mySampleText -> My Sample Text
Practical example
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.
// ONLINE-RUNNER:browser;
const toSentenceCase = camelCase => {
if (camelCase) {
const result = camelCase.replace(/([A-Z])/g, ' $1');
return result[0].toUpperCase() + result.substring(1).toLowerCase();
}
return '';
};
// Usage example:
console.log( toSentenceCase('mySampleText') ); // My sample text
console.log( toSentenceCase('anotherText') ); // Another text