JavaScript Program to Capitalize the First Letter of Every Sentence in a String

In this article, we will see how Capitalizing the first letter of every sentence in a string is a common text manipulation task in JavaScript. It involves identifying sentence boundaries and ensuring that the first letter of each sentence is in uppercase.

Examples:

Input: "this is Beginner for Beginner website. hello there!"
Output: "This is Beginner for Beginner website. Hello there!"
Input: "hello. how are you."
Output: "Hello. How are you."

Table of Content

  • Method 1: Using Regular Expressions:
  • Method 2: Using slice() Method
  • Method 3: Using forEach and String Manipulation

Method 1: Using Regular Expressions:

  • Split the text into sentences using a regular expression.
  • Capitalize the first letter of each sentence using a combination of toUpperCase() and substring().

Example: Here, is the implementation of the above approach

Javascript
function capitalizeSentences(text) {

    // Split the text into sentences 
    // using regular expressions
    const sentences = text.split(/\.|\?|!/);

    // Capitalize the first letter of each sentence
    const capitalizedSentences = sentences
        // Remove empty sentences
        .filter(sentence =>
            sentence.trim() !== '')
        .map(sentence =>
            sentence.trim()[0]
                .toUpperCase() +
            sentence.trim().slice(1));

    // Join the sentences back together
    return capitalizedSentences.join('. ') + '.';
}

const inputText =
    `Beginner for Beginner for 
     students.hello students.`;
const result = capitalizeSentences(inputText);
console.log(result);

Output
Beginner for Beginner for 
     students. Hello students.

Method 2: Using slice() Method

  • Split the text into an array of sentences using the split() method with sentence-ending punctuation marks as delimiters.
  • Capitalize the first letter of each sentence and join the sentences back together.

Example: Here, is the implementation of the above approach

Javascript
function capitalizeSentences(text) {
    const sentences =
        text.split(/[.!?]/)
            .filter(sentence =>
                sentence.trim() !== '');
    for (let i = 0; i < sentences.length; i++) {
        sentences[i] =
            sentences[i].trim()[0].toUpperCase() +
            sentences[i].trim().slice(1);
    }
    return sentences.join('. ') + '.';
}
const inputText = "Beginner for Beginner. hello students."
const result = capitalizeSentences(inputText);
console.log(result);

Output
Beginner for Beginner. Hello students.

Method 3: Using forEach and String Manipulation

Using forEach loop and string manipulation to capitalize the first letter of every sentence involves splitting the string into an array of sentences, iterating over each sentence with forEach, and capitalizing the first letter before joining the array back into a string.

Example:

JavaScript
function capitalizeSentences(str) {
  const sentences = str.split('. ');
  sentences.forEach((sentence, index) => {
    sentences[index] = sentence.charAt(0).toUpperCase() + sentence.slice(1);
  });
  return sentences.join('. ');
}

const inputString = "hello. how are you? i am fine.";
const capitalizedString = capitalizeSentences(inputString);
console.log(capitalizedString);

Output
Hello. How are you? i am fine.




Contact Us