开发者

Regex - replace string - no spaces at charAt(0)

开发者 https://www.devze.com 2023-04-04 16:38 出处:网络
return str.replace(/\\w\\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}).replace(/\\s/, \'\');
    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

精彩评论

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