6

我希望输入的字符串在任何情况下都应转换为句子大小写。

大家好,这是derp。谢谢大家回答我的问题。

转换为

大家好,这是derp。谢谢大家回答我的问题。

4

10 回答 10

18

我想出了这种正则表达式:

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();
});
于 2013-09-30T08:29:51.830 回答
7

试试这个,它会为你工作的。它也适用于具有前导空格的字符串

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);
于 2013-09-30T10:33:37.220 回答
2

这是我对这篇文章的修改,用于更改为标题案例。

您可以立即toLowerCase输入字符串,然后只toUpperCase输入每个单词的第一个字母。变成了一个很简单的 1 班轮:

而不是每一个字都做到。此示例与多行和字符串兼容A.M.P.M.当然,任何以句点和空格字符开头的单词。

toLowerCaseNames您可以在该函数下方和下面的示例中添加您自己的自定义词toUpperCaseNames

// 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. L.M.A.O.\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*([a-z])|^[a-z]/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))));


您可以将以上所有这些正则表达式粘贴到https://regexr.com/以分解它们的工作方式。

于 2019-06-27T14:20:32.047 回答
1

试用演示

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

请注意,所有点并不总是代表行尾,可能是缩写等。此外,它不确定是否在句号后键入空格。这些条件使该脚本易受攻击。

于 2013-09-30T08:39:06.667 回答
1

以下 SentenceCase 代码对我来说很好用,也可以处理 egam 等缩写。可能需要改进。

//=============================
// SentenceCase Function
// Copes with abbreviations
// Mohsen Alyafei (12-05-2017)
//=============================

function stringSentenceCase(str) {
  return str.replace(/\.\s+([a-z])[^\.]|^(\s*[a-z])[^\.]/g, s => s.replace(/([a-z])/,s => s.toUpperCase()))
}
//=============================

console.log(stringSentenceCase(" start sentence.   second sentence . e.g. a.m. p.m."))
console.log(stringSentenceCase("first sentence. second sentence."))
console.log(stringSentenceCase("e.g. a.m. p.m. P.M. another sentence"))

于 2020-04-17T16:34:44.523 回答
0

你也可以试试这个

<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>
于 2013-09-30T10:51:17.730 回答
0

这是我最终使用的解决方案:

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;
于 2013-11-26T16:07:59.997 回答
0

下面的代码按预期为我工作。

   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;
 }
于 2015-12-06T12:23:39.040 回答
0

我编写了一个基于 FSM 的函数来合并多个空格字符并将字符串转换为句子大小写。它应该很快,因为它不使用复杂的正则表达式,或者split假设您的 JavaScript 运行时具有有效的字符串连接,那么这应该是最快的方法。它还可以让您轻松添加特殊情况的异常。

通过用比较字符代码的函数替换空白正则表达式可能会进一步提高性能。

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;
}
于 2016-09-13T23:03:10.163 回答
0

该脚本将在每一行打印..... 星期日 星期一 星期二 星期三 星期四 星期五 星期六。

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();
}))
于 2018-05-06T20:05:24.790 回答