在字符串中大写单词的最佳方法是什么?
当前回答
还有loctus: https://locutus.io/php/strings/ucwords/,它是这样定义的:
function ucwords(str) {
// discuss at: http://locutus.io/php/ucwords/
// original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// improved by: Waldo Malqui Silva (http://waldo.malqui.info)
// improved by: Robin
// improved by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// bugfixed by: Cetvertacov Alexandr (https://github.com/cetver)
// input by: James (http://www.james-bell.co.uk/)
// example 1: ucwords('kevin van zonneveld')
// returns 1: 'Kevin Van Zonneveld'
// example 2: ucwords('HELLO WORLD')
// returns 2: 'HELLO WORLD'
// example 3: ucwords('у мэри был маленький ягненок и она его очень любила')
// returns 3: 'У Мэри Был Маленький Ягненок И Она Его Очень Любила'
// example 4: ucwords('τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός')
// returns 4: 'Τάχιστη Αλώπηξ Βαφής Ψημένη Γη, Δρασκελίζει Υπέρ Νωθρού Κυνός'
return (str + '').replace(/^(.)|\s+(.)/g, function ($1) {
return $1.toUpperCase();
});
};
其他回答
function capitalize(s){
return s.toLowerCase().replace( /\b./g, function(a){ return a.toUpperCase(); } );
};
capitalize('this IS THE wOrst string eVeR');
输出:“这是有史以来最糟糕的字符串”
更新:
这个解决方案似乎取代了我的:https://stackoverflow.com/a/7592235/104380
http://www.mediacollege.com/internet/javascript/text/case-capitalize.html是众多答案中的一个。
谷歌可以是解决此类问题所需的全部内容。
naïve方法是用空格分隔字符串,结果数组中每个元素的首字母大写,然后将它们重新连接起来。这就保留了现有的大写(例如,HTML仍然是HTML,而不会变成像HTML那样愚蠢的东西)。如果不希望产生这种影响,请在拆分字符串之前将整个字符串转换为小写字母。
只要输入字符串中没有重音字母,vsync提供的答案就可以工作。
我不知道原因,但显然\b在regexp匹配也重音字母(在IE8和Chrome上测试),所以像“località”这样的字符串会被错误地大写转换为“LocalitÀ”(à字母被大写,因为regexp认为这是一个单词边界)
一个更通用的函数也适用于重音字母是这样的:
String.prototype.toCapitalize = function()
{
return this.toLowerCase().replace(/^.|\s\S/g, function(a) { return a.toUpperCase(); });
}
你可以这样使用它:
alert( "hello località".toCapitalize() );
使用JavaScript和html
String.prototype.capitalize = function() { 返回this.replace (/ (^ | \ s) ([a - z]) / g函数(m, p1, p2) { return p1 + p2.toUpperCase(); }); }; <form name="form1" method="post"> <input name="instring" type="text" value="this is the text string" size="30"> <input type="button" name="Capitalize" value="Capitalize >>" onclick="form1.outstring.value=form1.instring.value.capitalize();" > <input name="outstring" type="text" value="" size="30"> > < /形式
基本上,你可以用string。capitalize()它会大写每个单词的第一个字母。
来源:http://www.mediacollege.com/internet/javascript/text/case-capitalize.html
我的解决方案:
String.prototype.toCapital = function () {
return this.toLowerCase().split(' ').map(function (i) {
if (i.length > 2) {
return i.charAt(0).toUpperCase() + i.substr(1);
}
return i;
}).join(' ');
};
例子:
'álL riGht'.toCapital();
// Returns 'Áll Right'
推荐文章
- Javascript和regex:分割字符串并保留分隔符
- 如何检查DST(日光节约时间)是否有效,如果是,偏移量?
- 如何打破_。在underscore.js中的每个函数
- 如何在jQuery中获得当前日期?
- 如何创建一个日期对象从字符串在javascript
- 输入触发器按钮单击
- 获取对象的属性名
- 如何检查用户是否可以回到浏览器历史
- 在Android上将字符串转换为整数
- 删除字符串的最后3个字符
- 相当于字符串。jQuery格式
- 如何在vue-cli项目中更改端口号
- Angular 2模板中的标签是什么意思?
- JavaScript .includes()方法的多个条件
- 如何大写一个字符串的第一个字母在省道?