Regex - replace string - no spaces at charAt(0)
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 th开发者_StackOverflow社区e string (they can be in the substring)
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'
To remove spaces at the start of a string in JavaScript
str.replace(/^\s+/, '')
精彩评论