![](/img/trans.png)
[英]How can I auto capitalize the first word of a sentence using Ckeditor?
[英]How to Capitalize first word of Every Sentence in JavaScript
我想要使用 JavaScript 大寫的所有句子的第一個字母,就像在每個句點 (.) 之后的第一個字符是大寫的。
像這樣:
這是第一句話。 這是第二句。 這是第三句話
以下代碼應該可以很好地工作:
text = text.replace(/(\.\s)([a-z])/g, (_, punctuation, char) => {
return punctuation + char.toUpperCase();
});
正則表達式與兩個捕獲組一起使用, String.prototype.replace與替換 function 一起使用。
const txt = "this is the first sentence. this is the second sentence. this is the third sentence"; const _txt = txt.split('.'); // split the the text into array by "." const _finalText = []; for(const tx of _txt) { //loop through the array const _tx = tx.trim(); //trim the element since some will contain space. const fl = _tx[0].toUpperCase(); //take the first character of the sentence and capitalize it. _finalText.push(fl+_tx.substr(1)) //push the result of the concatenation of the first letter(fl) and remaining letters without the first letter into the array; } console.log(_finalText.join('. ')) // This is the first sentence. This is the second sentence. This is the third sentence
let mySentences = "this is the first sentence. this is the second sentence. this is the third sentence" let newSentences = ""; // You can use this simple code mySentences.split(".").map(elem => newSentences += (elem.trim().charAt(0).toUpperCase() + elem.trim().slice(1, elem.length) + ".")) console.log(newSentences)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.