Jason
Jason

Reputation: 7682

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 the string (they can be in the substring)

Upvotes: 0

Views: 477

Answers (2)

jmar777
jmar777

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

Tomalak
Tomalak

Reputation: 338208

To remove spaces at the start of a string in JavaScript

str.replace(/^\s+/, '')

Upvotes: 2

Related Questions