【问题标题】:Capitalize words in title page until hyphen将标题页中的单词大写直到连字符
【发布时间】:2012-07-06 20:12:03
【问题描述】:

这是我页面的标题:

<title>john smith - Site and site - jobs</title>

在第一个 hifen (-) 之前,我必须将页面标题大写。这是我的代码,但丢失了第二部分和第一个连字符。

function toTitleCase(str){
    var str  = document.title;
    subTitle = str.split('-')[0];
    return str.substring(0,str.indexOf('-')).replace(/\w\S*/g, function(txt){
        return txt.charAt(0).toUpperCase() + txt.substring(1);
    });
}
document.title = toTitleCase(document.title);

【问题讨论】:

  • 如果你在函数的第 1 行覆盖它,为什么还要将 str 参数传递给函数?
  • @roybatty 你的正确输出是什么-> JOHN SMITH,或John Smith?你说的“capitalize”是什么意思?
  • 大写通常是第一个字母,不是全部,这是我的回答所依据的理解,所以我希望这就是他的意思

标签: javascript jquery capitalize


【解决方案1】:

这可能会有所帮助..

function ok()
{
  var str  = document.title;
  document.title=str.substring(0, str.indexOf('-')).toUpperCase()+str.substring(str.indexOf('-'),str.length);

}

【讨论】:

    【解决方案2】:

    嘿,试试这个:http://jsfiddle.net/LK3Vd/

    如果我错过了什么,让我知道。

    希望这有助于:)

    代码

    var str = $('#foo').html();
    str = str.substring(0, str.indexOf('-'));
    
    str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
        return letter.toUpperCase();
    });
    

    【讨论】:

      【解决方案3】:

      总结答案“最好的”+我的一粒盐:

      String.prototype.capitalize = function () {
        return this.replace(/\b[a-z]/g, function ($0) { return $0.toUpperCase(); });
      };
      
      function capitalizeTitle()
      {
        document.title = document.title.replace(/^[^\-]*/, function($0) {
          return $0.capitalize();
        });
      }
      
      capitalizeTitle();
      

      【讨论】:

        【解决方案4】:

        此代码将帮助您。

        function toTitleCase(str) {
                subTitle = str.split('-')[0].capitalize();
                return subTitle + str.substring(subTitle.length);
            }
        
            String.prototype.capitalize = function () {
                return this.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
            }
        

        【讨论】:

          【解决方案5】:

          加入核 REGEX 路线总是很好......

          var str = "some words - are - here";
          console.log("this is - a - string".replace(/^[^\-]*/, function($0) {
              return $0.replace(/\b[a-z]/g, function($0) { return $0.toUpperCase(); });
          }));
          

          输出:

          "Some Words - are - here"
          

          【讨论】:

          • 不错。但是/^[^\-]*/g 似乎对于第一个正则表达式就足够了。
          • 啊,是的,当然。我在之前的尝试中错误地离开了前瞻。感谢您的提醒。
          【解决方案6】:
          function toTitleCase(str){
              str = str.split('-');
              str[0]=str[0].replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
              return str.join("-");
              }
          document.title = toTitleCase(document.title);
          

          【讨论】:

          • 你会做JOHN SMITH,而不是John Smith
          • 他的问题是关于第二部分丢失了
          • 不要忘记点击答案右侧的复选标记
          猜你喜欢
          • 2022-07-04
          • 1970-01-01
          • 2018-08-06
          • 1970-01-01
          • 2016-02-15
          • 2016-09-06
          • 2016-10-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多