Form the bottom line to smooth the beginning of each sentence

I have a line like

FIRST SENTENCE. SECOND SENTENCE.

I want to write a string in such a way as to use the first letter of each sentence.

For example:

string = string.toLowerCase().capitalize();

only the first sentence is uppercase.

I have

String.prototype.capitalize = function () {return this.charAt (0) .toUpperCase () + this.slice (1); }

function

Does anyone know how to solve this?

+5
source share
4 answers

If you want to use only the first word of each sentence (not every word), use this function:

function applySentenceCase(str) {
    return str.replace(/.+?[\.\?\!](\s|$)/g, function (txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}

Jsfiddle here

txt.substr(1).toLowerCase() , txt.substr(1).toLowerCase() txt.substr(1)

+10

function toTitleCase(str) {
    return str.replace(/\w\S*/g, function (txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
    });
}

alert(toTitleCase('FIRST SENTENCE. SECOND SENTENCE.'))

DEMO

+4

I think this will work for you.

<a style="cursor:pointer;" onclick="capitaliseFirstLetter('hey wassup baby')">asd</a>

<div type="text" id="texts"></div>

Javascript

    function capitaliseFirstLetter(string)
{
    var array = string.split(" ");
    for(i=0;i<array.length;i++){
        var n = array[i];
        var a = n.charAt(0).toUpperCase() + n.slice(1);
        alert(a);
    }

}

visit: http://jsfiddle.net/rVnFU/

0
source

This JS function will apply the register of sentences to the original sentence and any sentences that follow a sentence that ends in. ? !

function applySentenceCase(str) {
    var txt = str.split(/(.+?[\.\?\!](\s|$))/g);
    for (i = 0; i < (txt.length-1); i++) {
        if (txt[i].length>1){
            txt[i]=txt[i].charAt(0).toUpperCase() + txt[i].substr(1).toLowerCase();
        } else if (txt[i].length==1) {
            txt[i]=txt[i].charAt(0).toUpperCase();
        }
    }
    txt = txt.join('').replace(/\s\s/g,' ');
    return txt;
}

alert(applySentenceCase("LOREM IPSUM DOLOR SIT AMET, CONSECTETUR ADIPISCING ELIT. sed congue hendrerit risus, ac viverra magna elementum in. InTeRdUm Et MaLeSuAdA fAmEs Ac AnTe IpSuM pRiMiS iN fAuCiBuS. phasellus EST purus, COMMODO vitae IMPERDIET eget, ORNARE quis ELIT."));
Run codeHide result
0
source

All Articles