My JS woks well when the string has one word:
It works well also when the string is a space sEparated words :
But when it's an underscore separated words i got? :
My purpose is to generalize it to get that :
How do I make it become LIKE THAT?
My script is that :
capitalizeString(str) {
var lowerString = str.toLowerCase();
return lowerString.replace(/(^| )(\w)/g, (x) => {
return x.toUpperCase();
});
}
You can add the hyphen (-
) as part of the RegEx:
function capitalizeString(str) {
var lowerString = str.toLowerCase();
return lowerString.replace(/(^|[ -])(\w)/g, (x) => {
return x.toUpperCase();
});
}
console.log(capitalizeString('BRUNO'));
console.log(capitalizeString('JEAN MARC'));
console.log(capitalizeString('JEAN-FRANCOIS'));