xxxxxxxxxx
export function capitalize(str: string, all: boolean = false) {
if (all)
return str.split(' ').map(s => capitalize(s)).join(' ');
return str.charAt(0).toUpperCase() + str.slice(1);
}
xxxxxxxxxx
//capitalize only the first letter of the string.
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
//capitalize all words of a string.
function capitalizeWords(string) {
return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
xxxxxxxxxx
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
xxxxxxxxxx
//Updated
//capitalize only the first letter of the string.
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
//capitalize all words of a string.
function capitalizeWords(string) {
return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
xxxxxxxxxx
const capitalizeFirstLetter(string) =>
string.charAt(0).toUpperCase() + string.slice(1).toLowerCase()
xxxxxxxxxx
myString = 'the quick green alligator...';
myString.replace(/^\w/, (c) => c.toUpperCase());
myString = ' the quick green alligator...';
myString.trim().replace(/^\w/, (c) => c.toUpperCase());
xxxxxxxxxx
function capitalizeWords(string) {
return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
xxxxxxxxxx
function capitalize (value:string) {
var textArray = value.split(' ')
var capitalizedText = ''
var conjunctions = ['the', 'of', 'a']
for (var i = 0; i < textArray.length; i++) {
if (conjunctions.includes(textArray[i])) {
continue
}
capitalizedText += textArray[i].charAt(0).toUpperCase() + textArray[i].slice(1) + ' '
}
return capitalizedText.trim()
}
xxxxxxxxxx
const mySentence = "freeCodeCamp is an awesome resource";
const finalSentence = mySentence.replace(/(^\w{1})|(\s+\w{1})/g, letter => letter.toUpperCase());