【问题标题】:Format lowercased string to capitalize the begining of each sentence格式化小写字符串以大写每个句子的开头
【发布时间】:2013-12-24 20:46:15
【问题描述】:

我有一个类似

的字符串

FIRST SENTENCE. SECOND SENTENCE.

我想以这种方式小写字符串以大写每个句子的第一个字母。

例如:

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

只有第一句大写。

我有

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

功能

有人知道怎么解决吗?

【问题讨论】:

    标签: javascript regex string lowercase capitalization


    【解决方案1】:

    如果你只想将每个句子的第一个单词(不是每个单词)都大写,那么使用这个函数:

    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)

    【讨论】:

    • 如果我使用 replace(/\w\S*/g, function (txt) 而不是你的代码有什么区别?谢谢。
    • @zsola3075457 这将在每个单词的开头添加一个大写字母:jsfiddle.net/VAKk8/1
    • 使用/.+?([\.\?\!]\s|$)/g 使其也适用于没有标点符号的单句/结尾句
    • 函数 applySentenceCase(str) { return str.toLowerCase().replace(/(^.|\.\s.|\!\s.|\?\s.)/g, 函数(txt) { 返回 txt.toUpperCase(); }); }
    【解决方案2】:

    试试这个

    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

    Reference

    【讨论】:

      【解决方案3】:

      此 JS 函数会将句子大小写应用于初始句子和以 . 结尾的句子之后的任何句子。 ? !

      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."));

      【讨论】:

        【解决方案4】:

        我认为这对你有用

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

        访问:http://jsfiddle.net/rVnFU/

        【讨论】:

        • 对不起,我忘记了,但是我已经有这个功能了,这里只大写第一句。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 2014-12-06
        • 1970-01-01
        • 1970-01-01
        • 2015-11-28
        • 2021-02-26
        相关资源
        最近更新 更多