獲取一些代碼以便將字符串中每個單詞的首字母大寫。有人可以幫助我更新它,這樣它也可以刪除字符串中的所有空格,一旦第一個字母被封閉。首字母大寫並刪除字符串中的空格
return function (str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1);
});
}
獲取一些代碼以便將字符串中每個單詞的首字母大寫。有人可以幫助我更新它,這樣它也可以刪除字符串中的所有空格,一旦第一個字母被封閉。首字母大寫並刪除字符串中的空格
return function (str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1);
});
}
試試這個:
var input = 'lorem ipsum dolor sit amet';
var output = input.replace(/\w+/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1);
}).replace(/\s/g, '');
document.getElementById('output').innerHTML = output;
<div id="output"></div>
工作過很多感謝。決定接受這個答案,因爲它對於尋找這個答案的其他開發者來說更加徹底和理想:) – Richy
@Richy好吧。請享用。但是,我之前首先給出了完全相同的答案,所以如果你認爲公平,只有我沒有做的事情是提供一個片段。對?我會確保我從下一步開始。 –
頁對如何爲每個字一個很好的例子在JavaScript中的字符串中:http://alvinalexander.com/javascript/how-to-capitalize-each-word-javascript-string
[This](http://codereview.stackexchange.com/questions/77614/capitalize-the-first-character-of-all-words-even-when-following-a)解釋瞭如何將每個首字母大寫。 –
可能重複[在JavaScript中首字母大寫](http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript) –