开发者

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+/, '')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜