大写用句点分隔的句子中的第一个单词javascript



使用for或while循环,但不使用Regex,我必须将字符串中句子中的第一个单词大写。句子用点分隔。

目前我有以下

<p id="tekst">This is an. Example text. To showcase. What i want</p>
function highlight()
{
var text = document.getElementById("tekst").innerHTML;
//split the above string into an array of strings 
//whenever a blank space is encountered
let arr = text.split(" ");
//loop through each element of the array and capitalize the first letter.
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
}
//Join all the elements of the array back into a string 
//using a blankspace as a separator 
const str2 = arr.join(" ");
console.log(str2);
}

目前的做法是将单词中的第一个字母大写。所以文本示例是

这是一个示例文本。展示。我想要什么

所需的结果是

这是一个示例文本。TO展示。我想要什么

我希望下面的注释代码能有所帮助:

function highlight() {
var text = document.getElementById("tekst").innerHTML;
let arr = text.split(". ");
let newSentence = ''
//loop through each element of the array and capitalize the first letter.
for (var i = 0; i < arr.length; i++) {
//splitting words
const words = arr[i].split(' ')
//getting the first word and capitalise it
words[0] = words[0].toUpperCase()
//removing the first word from the array and adding the rest of the words to newSentence and adding '. ' to the end of sentence
newSentence += words.join(' ') + '. '
}
//trim the sentence to remove any space in the end
newSentence = newSentence.trim()
console.log(newSentence);
}

您必须先在.上拆分,而不是在(空间)上拆分。这样,您就可以获得一个数组,而不仅仅是通过循环

function highlight() {
var text = document.getElementById("tekst").innerHTML;
//split the above string into an array of strings 
//whenever a blank space is encountered along with full stop('.')
let arr = text.split(". ");
let arr2;
let fullSentence = '';

//loop through each element of the array and capitalize the first letter.
for (var i = 0; i < arr.length; i++) {
arr2 = arr[i].split(" ");
arr2[0] = arr2[0].toUpperCase();

//get all values from array2 and than join them with a space and end them with '.' . As this is looping so value will be saved in fullSentence and with `+` sign each subsequent value will be joined with previous one
fullSentence += arr2.join(' ') + '. '
}
console.log(fullSentence);
}
highlight();// for automatic run of function
<p id="tekst">This is an. Example text. To showcase. What i want</p>

因为循环对我来说总是很痛苦,所以得到了@Elson Ramos 的帮助

这样的东西怎么样?

function highlight() {
var text = "This is an. Example text. To showcase. What i want";
//split the above string into an array of strings 
//whenever a blank space is encountered
let arr = text.split(".");
//loop through each element of the array and capitalize the first letter.
arr.forEach((string, index) => {
let sep = string.trim().split(" ");
sep[0] = sep[0].toUpperCase()
arr[index] = sep.join(" ");
});
//Join all the elements of the array back into a string 
//using a blankspace as a separator 
const str2 = arr.join(". ");
console.log(str2);
}

希望这样的东西能有所帮助!

function formatSentence(sentence) {
const words = sentence.split(" ");
const firstWord = words.shift().toUpperCase();
return [firstWord, ...words.map(w => w.toLowerCase())].join(" ");
}
function formatParagraph(str) {
const sentences = str.split(". ");
const processed = sentences.map(formatSentence);
return processed.join(". ");
}
console.log(
formatParagraph("This Is An. Example Text. To Showcase. What I Want")
);

最新更新