Convert string to sentence case in javascript

2020-02-01 05:51发布

I want a string entered should be converted to sentence case in whatever case it is.

Like

hi all, this is derp. thank you all to answer my query.

be converted to

Hi all, this is derp. Thank you all to answer my query.

9条回答
Summer. ? 凉城
2楼-- · 2020-02-01 06:45

I came up with this kind of RegExp:

var rg = /(^\w{1}|\.\s*\w{1})/gi;
var myString = "hi all, this is derp. thank you all to answer my query.";
myString = myString.replace(rg, function(toReplace) {
    return toReplace.toUpperCase();
});
查看更多
我只想做你的唯一
3楼-- · 2020-02-01 06:48

This is the solution I ended up using:

str = 'hi all, this is derp. thank you all to answer my query.';
temp_arr = str.split('.');
for (i = 0; i < temp_arr.length; i++) {
temp_arr[i]=temp_arr[i].trim()
temp_arr[i] = temp_arr[i].charAt(0).toUpperCase() + temp_arr[i].substr(1).toLowerCase();
}
str=temp_arr.join('. ') + '.';
return str;
查看更多
Bombasti
4楼-- · 2020-02-01 06:52

I wrote an FSM-based function to coalesce multiple whitespace characters and convert a string to sentence-case. It should be fast because it doesn't use complex regular-expression or split and assuming your JavaScript runtime has efficient string concatenation then this should be the fastest way to do it. It also lets you easily add special-case exceptions.

Performance can probably be improved further by replacing the whitespace regexs with a function to compare char-codes.

function toSentenceCase(str) {

    var states = {
        EndOfSentence  : 0,
        EndOfSentenceWS: 1, // in whitespace immediately after end-of-sentence
        Whitespace     : 2,
        Word           : 3
    };

    var state = states.EndOfSentence;
    var start = 0;
    var end   = 0;

    var output = "";
    var word = "";

    function specialCaseWords(word) {
        if( word == "i" ) return "I";
        if( word == "assy" ) return "assembly";
        if( word == "Assy" ) return "Assembly";
        return word;
    }

    for(var i = 0; i < str.length; i++) {

        var c = str.charAt(i);

        switch( state ) {
            case states.EndOfSentence:

                if( /\s/.test( c ) ) { // if char is whitespace

                    output += " "; // append a single space character
                    state = states.EndOfSentenceWS;
                }
                else {
                    word += c.toLocaleUpperCase();
                    state = states.Word;
                }

                break;

            case states.EndOfSentenceWS:

                if( !( /\s/.test( c ) ) ) { // if char is NOT whitespace

                    word += c.toLocaleUpperCase();
                    state = states.Word;
                }

                break;
            case states.Whitespace:

                if( !( /\s/.test( c ) ) ) { // if char is NOT whitespace

                    output += " "; // add a single whitespace character at the end of the current whitespace region only if there is non-whitespace text after.
                    word += c.toLocaleLowerCase();
                    state = states.Word;
                }

                break;

            case states.Word:

                if( c == "." ) {

                    word = specialCaseWords( word );
                    output += word;
                    output += c;
                    word = "";
                    state = states.EndOfSentence;

                } else if( !( /\s/.test( c ) ) ) { // if char is NOT whitespace

                    // TODO: See if `c` is punctuation, and if so, call specialCaseWords(word) and then add the puncutation

                    word += c.toLocaleLowerCase();
                }
                else {
                    // char IS whitespace (e.g. at-end-of-word):

                    // look at the word we just reconstituted and see if it needs any special rules
                    word = specialCaseWords( word );
                    output += word;
                    word = "";

                    state = states.Whitespace;
                }

                break;
        }//switch
    }//for

    output += word;

    return output;
}
查看更多
登录 后发表回答