Uppercase letters on the title page before the hyphen

This is the name of my page:

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

I need to smooth the page title to the first hifen (-). This is my code, but lost the second part and the first hyphen.

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);
+5
source share
6 answers
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);
+1
source

It's always good to drop the REGEX nuclear route ...

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

Outputs:

"Some Words - are - here"
+1
source

" " + :

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

.

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

, : http://jsfiddle.net/LK3Vd/

lemme , - .

, :)

var str = $('#foo').html();
str = str.substring(0, str.indexOf('-'));

str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
    return letter.toUpperCase();
});
0

.

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

}

0

All Articles