Reputation: 7682
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}).replace(/\s/, '');
I just want to prevent any spaces at the beginning of the string (they can be in the substring)
Upvotes: 0
Views: 477
Reputation: 39649
There's trim
, as Dani mentioned, but that removes spaces from both ends of the string. If it must be just the start of the string you can do this:
' foo bar'.replace(/^\s+/, ''); // 'foo bar'
Upvotes: 2
Reputation: 338208
To remove spaces at the start of a string in JavaScript
str.replace(/^\s+/, '')
Upvotes: 2