简体   繁体   中英

Convert string to sentence case in javascript

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.

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();
});

Try this, It will work fine for you. It will also work for String having leading spaces .

var string="hi all, this is derp. thank you all to answer my query.";
var n=string.split(".");
var vfinal=""
for(i=0;i<n.length;i++)
{
   var spaceput=""
   var spaceCount=n[i].replace(/^(\s*).*$/,"$1").length;
   n[i]=n[i].replace(/^\s+/,"");
   var newstring=n[i].charAt(n[i]).toUpperCase() + n[i].slice(1);
   for(j=0;j<spaceCount;j++)
   spaceput=spaceput+" ";
   vfinal=vfinal+spaceput+newstring+".";
 }
 vfinal=vfinal.substring(0, vfinal.length - 1);
 alert(vfinal);

Here's my modification of this post which was for changing to Title Case.

You could immediately toLowerCase the string, and then just toUpperCase the first letter of each word. Becomes a very simple 1 liner:

Instead of making it every word. This example is compatible with multiple lines and strings like AM and PM and of course, any word proceeding a period and a whitespace character.

You could add your own custom words below that toLowerCaseNames function and toUpperCaseNames in that example below.

 // Based off this post: https://stackoverflow.com/a/40111894/8262102 var str = '-------------------\\nhello world!\\n\\n2 Line Breaks. What is going on with this string. LMAO\\n\\nThee End...\\nlower case example 1\\nlower case example 2\\n-------------------\\nwait there\\'s more!\\n-------------------\\nhi all, this is derp. thank you all to answer my query.'; function toTitleCase(str) { return str.toLowerCase().replace(/\\.\\s*([az])|^[az]/gm, s => s.toUpperCase()); } // Add your own names here to override to lower case function toLowerCaseNames(str) { return str.replace(/\\b(lower case example 1|lower case example 2)\\b/gmi, s => s.toLowerCase()); } // Add your own names here to override to UPPER CASE function toUpperCaseNames(str) { return str.replace(/\\b(hello|string)\\b/gmi, s => s.toUpperCase()); } console.log(toLowerCaseNames(toUpperCaseNames(toTitleCase(str))));


You can paste all those regexp above into https://regexr.com/ to break down how they work.

Try Demo

http://jsfiddle.net/devmgs/6hrv2/

function sentenceCase(strval){

 var newstrs = strval.split(".");
    var finalstr="";
    //alert(strval);
    for(var i=0;i<newstrs.length;i++)
        finalstr=finalstr+"."+ newstrs[i].substr(0,2).toUpperCase()+newstrs[i].substr(2);
    return finalstr.substr(1);
}

Beware all dot doesn't always represent end of line and may be abbreviations etc. Also its not sure if one types a space after the full stop. These conditions make this script vulnerable.

The following SentenceCase code works fine for me and also handles abbreviations such egam and so on. May require improvements.

 //============================= // SentenceCase Function // Copes with abbreviations // Mohsen Alyafei (12-05-2017) //============================= function stringSentenceCase(str) { return str.replace(/\\.\\s+([az])[^\\.]|^(\\s*[az])[^\\.]/g, s => s.replace(/([az])/,s => s.toUpperCase())) } //============================= console.log(stringSentenceCase(" start sentence. second sentence . egampm")) console.log(stringSentenceCase("first sentence. second sentence.")) console.log(stringSentenceCase("egampm PM another sentence"))

You can also try this

<script>
var name="hi all, this is derp. thank you all to answer my query.";
var n = name.split(".");
var newname="";
for(var i=0;i<n.length;i++)
{
var j=0;
while(j<n[i].length)
{
if(n[i].charAt(j)!= " ")
    {
        n[i] = n[i].replace(n[i].charAt(j),n[i].charAt(j).toUpperCase());
            break;
    }
else
  j++;
}

newname = newname.concat(n[i]+".");
 }
alert(newname);
 </script>

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;

The below code is working for me as expected.

   function toSentenceCase(inputString) {
        inputString = "." + inputString;
   var result = "";
   if (inputString.length == 0) {
       return result;
   }

   var terminalCharacterEncountered = false;
   var terminalCharacters = [".", "?", "!"];
   for (var i = 0; i < inputString.length; i++) {
       var currentChar = inputString.charAt(i);
       if (terminalCharacterEncountered) {
           if (currentChar == ' ') {
               result = result + currentChar;
           } else {
               var currentCharToUpperCase = currentChar.toUpperCase();
               result = result + currentCharToUpperCase;
               terminalCharacterEncountered = false;
           }
       } else {
           var currentCharToLowerCase = currentChar.toLowerCase();
           result = result + currentCharToLowerCase;
       }
       for (var j = 0; j < terminalCharacters.length; j++) {
           if (currentChar == terminalCharacters[j]) {
               terminalCharacterEncountered = true;
               break;
           }
       }
   }
        result = result.substring(1, result.length - 1);
   return result;
 }

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;
}

On each line this script will print ..... Sunday Monday Tuesday Wednesday Thursday Friday Saturday.

let rg = /(^\w{1}|\.\s*\w{1})/gi;

const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

for(let day of days) {
    console.log(day.replace(rg, function(toReplace) {
    return toReplace.toUpperCase();
}))
function toTitleCase(str) {
  if (!str) return "";
  return str
    .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
      return index === 0 ? word.toUpperCase() : word.toLowerCase();
    })
    .replace(/\s+/g, "");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM