Skip to content Skip to sidebar Skip to footer

Sentencecase In Javascript

I want to make sentence case with JavaScript. Input: hello world example I have tried this so far: $('.sentenceCase').click(function(event) { var rg = /(^\w{1}|\.\s*\w{

Solution 1:

You only care if the letter is the first letter in a group, so...

/\b\w/g

Matches the word-character that comes after a word boundary - i.e. the first letter in each word.

Solution 2:

Try this:

$('.sentenceCase').click(function(event) {
    var rg = /(^\w{1}|\.\s*\w{1}|\n\s*\w{1})/gi;
    var textareaInput=$('.textareaInput').val();
    myString = textareaInput.replace(rg, function(toReplace) {
        return toReplace.toUpperCase();
    });
 $('.textareaInput').val(myString);
 });

In your code, you are checking for fullstop(.), but your text contains the new line character. That is the issue.

In this Regex, it will look for the first character in the beginning as well as after '.' and '\n' in the string.

Post a Comment for "Sentencecase In Javascript"