admin管理员组文章数量:1401670
I've been looking around on how to use JavaScript to set names to proper case, e.g. george mchall would bee George McHall. I was able to find a write up on Codeproject on how to do this, as well as a person that intended it to do this:
function toProperCase(s){
return s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
function($1, $2, $3, $4, $5) {
if($2){
return $3.toUpperCase()+$4+$5.toUpperCase();
}
return $1.toUpperCase();
});
}
This allows for what I'm looking for. But I need to be able to extend it further and add additional cases.
I found another page on John Gruber's site doing title case, but I'm only looking at doing names.
So, does anyone have an idea on extending it? I'm really just looking for a point in the right direction.
Edit: Since I seem to be hitting a wall here, maybe someone has a way to do it server side. This is at least for now using ColdFusion for the server side. I've seen a C# implementation, but I'm not able to move to C# at the moment.
I've been looking around on how to use JavaScript to set names to proper case, e.g. george mchall would bee George McHall. I was able to find a write up on Codeproject on how to do this, as well as a person that intended it to do this:
function toProperCase(s){
return s.toLowerCase().replace( /\b((m)(a?c))?(\w)/g,
function($1, $2, $3, $4, $5) {
if($2){
return $3.toUpperCase()+$4+$5.toUpperCase();
}
return $1.toUpperCase();
});
}
This allows for what I'm looking for. But I need to be able to extend it further and add additional cases.
I found another page on John Gruber's site doing title case, but I'm only looking at doing names.
So, does anyone have an idea on extending it? I'm really just looking for a point in the right direction.
Edit: Since I seem to be hitting a wall here, maybe someone has a way to do it server side. This is at least for now using ColdFusion for the server side. I've seen a C# implementation, but I'm not able to move to C# at the moment.
Share Improve this question edited Nov 9, 2010 at 14:34 Tim Meers asked Nov 9, 2010 at 14:05 Tim MeersTim Meers 9281 gold badge15 silver badges24 bronze badges 11- 5 It's more plicated a problem than it may appear at first glance. You will have to allow for cases beyond Mac/Mc. For example, O'Brien, D'Agostino, LaSalle, etc. Regex may not prove to be the best tool for this. You may have to resort to some kind of dictionary. – Robusto Commented Nov 9, 2010 at 14:12
- It already accounts for names with ' and - because the regex is counting those as a new word after the hyphen or apostrophe. And the regex is already accounting for the Mc/Mac. I'm really looking to account for the La* cases. Anythinh I'm then missing I should know how to add once I figure out how to continue to extend it. It's also nothing mission critical, I'm just trying to catch as many as possible for a consistent output. – Tim Meers Commented Nov 9, 2010 at 14:23
- Consider also the various latin names, like "Oscar de la Renta" where it's incorrect for the "de" and "la" to be capitalized. – T.J. Crowder Commented Nov 9, 2010 at 14:24
- 1 @Tim: the problem is differentiating between surnames like Lasseter and LaSalle. That's not something you can do with regex. – Andy E Commented Nov 9, 2010 at 14:42
- 1 @Tim Meers: Just thought of another ugly exception for you. Google "Jerald terHorst", who was press secretary for Gerald Ford. I think you'll find there is no "perfect" way to acplish this with regular expressions. – Robusto Commented Nov 10, 2010 at 13:43
2 Answers
Reset to default 6Combining a few answers from similar posts:
var selElem = document.getElementById("fromName");
selElem.addEventListener("change", function() {
document.getElementById("result").innerHTML = properName(selElem.value);
});
function properName(name) {
return ("" + name.replace(/[^\s\-\']+[\s\-\']*/g, function(word) {
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
}).replace(/\b(Van|De|Der|Da|Von)\b/g, function(nobiliaryParticle) {
return nobiliaryParticle.toLowerCase();
}).replace(/Mc(.)/g, function(match, letter3) {
return 'Mc' + letter3.toUpperCase();
}));
}
<p>
Choose a name to see it properly capitalised:
</p>
<select id="fromName">
<option>Select a name</option>
<option>EMMA HURST</option>
<option>CHRIS HINDI</option>
<option>OSCAR GRENFELL</option>
<option>JIM CASEY</option>
<option>MEOW-LUDO DISCO GAMMA MEOW-MEOW</option>
<option>PAT SHEIL</option>
<option>NOEL MCFARLANE</option>
<option>CHRIS MCLACHLAN</option>
<option>ANTHONY ALBANESE</option>
<option>DAVID VAN GOGH</option>
<option>JAMIE ELVY</option>
</select>
<p>Result: <span id="result"></span></p>
How about this:
if (str==str.toLowerCase() || str==str.toUpperCase())
str = str.toTitleCase();
Otherwise, leave it well alone!!!
Edit: You could optionally split the string to weed out people holding the shift key for too long, like SMith.
本文标签: regexHow can I put names into title case in JavaScriptStack Overflow
版权声明:本文标题:regex - How can I put names into title case in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744247382a2597085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论