Is there a way I can turn this string
let string = 'I have some spaces in it';
into
string = 'iHaveSomeSpacesInIt';
I know I can use
string.split(' ').join('');
to take all the spaces out of the string but how can I transform the first uppercase letter to lowercase and then camelCase at all the spaces that have been removed??
Any help would be appreciated!
Thanks
Maybe regex can help you lot more faster and produce a more clear code.
var regex = /\s+(\w)?/gi;
var input = 'I have some spaces in it';
var output = input.toLowerCase().replace(regex, function(match, letter) {
return letter.toUpperCase();
});
console.log(output);
Sure, just map each word (except the first) and capitalize the first letter:
const input = 'I have some spaces in it';
const output = input
.split(' ')
.map((word, i) => {
if (i === 0) return word.toLowerCase();
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join('');
console.log(output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With