return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}).replace(/\s/, '');
我只是想防止字符串开头出现任何空格(它们可以位于子字符串中)
最佳答案
正如 Dani 提到的,有 trim
,但这会删除字符串两端的空格。如果它必须只是字符串的开头,您可以这样做:
' foo bar'.replace(/^\s+/, ''); // 'foo bar'
关于javascript - 正则表达式 - 替换字符串 - charAt(0) 处没有空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418670/