text.replace(/(^w|sw)/g, m => m.toUpperCase());
// Explanation:
//
// ^w : first character of the string
// | : or
// sw : first character after whitespace
// (^w|sw) Capture the pattern.
// g Flag: Match all occurrences.
// Example usage:
// Create a reusable function:
const toTitleCase = str => str.replace(/(^w|sw)/g, m => m.toUpperCase());
// Call the function:
const myStringInTitleCase = toTitleCase(myString);
//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(); });
};
const capitalizeFirstLetter(string) =>
string.charAt(0).toUpperCase() + string.slice(1).toLowerCase()
function capitalizeWords(string) {
return string.replace(/(?:^|s)S/g, function(a) { return a.toUpperCase(); });
};
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);
}