Reputation: 191
This is the title of my page:
<title>john smith - Site and site - jobs</title>
I have to capitalize the title of the page until 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);
Upvotes: 5
Views: 329
Reputation: 2568
This might help..
function ok()
{
var str = document.title;
document.title=str.substring(0, str.indexOf('-')).toUpperCase()+str.substring(str.indexOf('-'),str.length);
}
Upvotes: 0
Reputation: 34107
Hey try this please: http://jsfiddle.net/LK3Vd/
lemme know if I missed anything.
Hope this helps :)
code
var str = $('#foo').html();
str = str.substring(0, str.indexOf('-'));
str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
});
Upvotes: 0
Reputation: 23041
Summarizing answers "best of" + my grain of salt :
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();
Upvotes: 0
Reputation: 7562
This code will help you.
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(); });
}
Upvotes: 0
Reputation: 34556
Always good to throw in a nuclear REGEX 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"
Upvotes: 1
Reputation:
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);
Upvotes: 1