I looked at some questions and answers about capitalize in StackOverflow but could not find answer about my problem.
I would like to capitalize first letter of each word in a string only if word lengh > 2.
My temporary solution was:
var str = str.toLowerCase().replace(/\b[a-z]/g, function (letter) {
return letter.toUpperCase();
}).replace(/\b\w{1,2}\b/g, function (letter) {
return letter.toLowerCase();
});
There is a solution that can unite the two regex in one?
I looked at some questions and answers about capitalize in StackOverflow but could not find answer about my problem.
I would like to capitalize first letter of each word in a string only if word lengh > 2.
My temporary solution was:
var str = str.toLowerCase().replace(/\b[a-z]/g, function (letter) {
return letter.toUpperCase();
}).replace(/\b\w{1,2}\b/g, function (letter) {
return letter.toLowerCase();
});
There is a solution that can unite the two regex in one?
Share Improve this question asked Jun 19, 2013 at 20:26 Conrado FonsecaConrado Fonseca 6622 gold badges7 silver badges19 bronze badges 4- What should happen in edge cases like "i want to be 'titelised'"? Should the quoted word remain unchanged? – Xotic750 Commented Jun 19, 2013 at 21:17
- Or others such as "19th amendment" or "train19", unfortunately language is irregular. – Xotic750 Commented Jun 19, 2013 at 21:26
- @Xotic750 The functionality is for to format names. – Conrado Fonseca Commented Jun 19, 2013 at 22:41
- 1 Aha, that gives us a little more insight into what you were trying to achieve, of course there are still many 2 character names out there, especially Chinese, "jo", "ia", "li" are just some examples. So I assume whitespace boundries would be enough in your use case? – Xotic750 Commented Jun 19, 2013 at 22:53
6 Answers
Reset to default 21This must do the job:
str = str.toLowerCase().replace(/\b[a-z](?=[a-z]{2})/g, function(letter) {
return letter.toUpperCase(); } );
or to deal with unicode letters:
str = str.toLowerCase().replace(/(?<!\p{L})\p{L}(?=\p{L}{2})/gu, m => m.toUpperCase());
[EDIT]
The first above example is a little naive since it assumes that there is only letters in the string, and doesn't take account that a word boundary \b
can match the limit between a word character [a-zA-Z0-9_]
and a non word character or an anchor. Thus, to be more rigorous, it's better to write:
str = str.toLowerCase().replace(/([^a-z]|^)([a-z])(?=[a-z]{2})/g, function(_, g1, g2) {
return g1 + g2.toUpperCase(); } );
If you want to do the same but this time, including the first letter of the string (whatever the number of letters after) you can use this:
str = str.toLowerCase().replace(/([^a-z])([a-z])(?=[a-z]{2})|^([a-z])/g, function(_, g1, g2, g3) {
return (typeof g1 === 'undefined') ? g3.toUpperCase() : g1 + g2.toUpperCase(); } );
Try this:
var str = str.toLowerCase().replace(/\b\w{3,}/g, function (l) {
return l.charAt(0).toUpperCase() + l.slice(1);
});
Here's a simpler solution which does the job.
const capitalize = (str) =>
str.toLowerCase().replace(/\w{3,}/g, (match) =>
match.replace(/\w/, (m) => m.toUpperCase()));
Maybe use a function to make it more clean
var capitalize1st = function(str){
str = str || '';
// string length must be more than 2
if(str.length<3){
return str;
}
return str[0].toUpperCase()+str.slice(1);
}
var splitWordsAndCap1st = function(str){
str = str || '';
var words = str.match(/\S+/g);
for(var i=0;i<words.length;i++){
words[i] = capitalize1st(words[i]);
}
return words.join(' ');
}
splitWordsAndCap1st("I would like to capitalize first letter of each word in a string");
No need for regex (just one small one to capture whitespace), you could do this
Javascript
function titleCaseLengthGt2(string) {
var array = string.split(/(\s+)/),
length = array.length,
i = 0,
word;
while (i < length) {
//array[i] = array[i].toLowerCase(); // make words lowercased first if you want
word = array[i];
if (word.length > 2) {
array[i] = word.charAt(0).toUpperCase() + word.slice(1);
}
i += 1;
}
return array.join("");
}
console.log(titleCaseLengthGt2("i want to be titelised"));
Output
i Want to be Titelised
on jsfiddle
To capitalize first letter of each word in a string if word length > 2, I use for english text:
l_text = l_text.toLowerCase().replace(/(?=\b)([a-z])(?=[a-z]{2})/g,
function(g0) {return (g0.toUpperCase());});
Often in real life the rule "word length > 2" is not enough.
To capitalize town names in french I have to exclude some words like in this sample:
l_text = "Bourg-en-Bresse, NEUILLY SUR SEINE, enghien-les-bains";
l_text = l_text.toLowerCase().replace(/(?=\b)(?!(?:d|en|les|sous|sur)\b)([a-z])/g,
function(g0) {return (g0.toUpperCase());});
For a more sophisticated situation, you may combine the negative lookahead assertion (?!(:|en|les|sous|sur)\b) and the positive lookahead assertion (?=[a-z]{2}).
To deal with non standard word boundary and characters out of a-z range, you may use a word boundary and characters set specific to the context:
/(?:^|[\s'\-])(?!(?:d|en|les|sous|sur)[\s'\-])([a-zàâéèêïôùûç])/g