是否有一个简单的方法来转换字符串标题大小写?例如,约翰·史密斯变成了约翰·史密斯。我不是在寻找像John Resig的解决方案那样复杂的东西,只是(希望)一些一两行代码。


当前回答

Var结果= “这很有趣”。/\b[a-z]/g, (x) => x. touppercase ()) console.log(result) // This Is Very Interesting

其他回答

与John Resig的解决方案一样功能齐全,但作为一行程序:(基于此github项目)

function toTitleCase(e){var t=/^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;return e.replace(/([^\W_]+[^\s-]*) */g,function(e,n,r,i){return r>0&&r+n.length!==i.length&&n.search(t)>-1&&i.charAt(r-2)!==":"&&i.charAt(r-1).search(/[^\s-]/)<0?e.toLowerCase():n.substr(1).search(/[A-Z]|\../)>-1?e:e.charAt(0).toUpperCase()+e.substr(1)})};

console.log( toTitleCase( "ignores mixed case words like iTunes, and allows AT&A and website.com/address etc..." ) );

实现类似功能的另一种方法如下所示。

formatName(name) {
    let nam = '';
    name.split(' ').map((word, index) => {
        if (index === 0) {
            nam += word.split('').map((l, i) => i === 0 ? l.toUpperCase() : l.toLowerCase()).join('');
        } else {
            nam += ' ' + word.split('').map(l => l.toLowerCase()).join('');
        }
    });
    return nam;
}

瓦尔·弦=“测试” 笨蛋。 var输出=弦。charAt(0) 控制台日志(输出)。 警报(输出)

  
 var string = "tEsT"

 string = string.toLowerCase() 

 string.charAt(0).toUpperCase() + string.slice(1)

string.charAt(0) returns the character at the 0th index of the string. toUpperCase() is a method that returns the uppercase equivalent of a string. It is applied to the first character of the string, returned by charAt(0). string.slice(1) returns a new string that starts from the 1st index (the character at index 0 is excluded) till the end of the string. Finally, the expression concatenates the result of toUpperCase() and string.slice(1) to create a new string with the first character capitalized.

更简单的高性能版本,具有简单的缓存。

var TITLE_CASE_LOWER_MAP = { 'a': 1, 'an': 1, 'and': 1, 'as': 1, 'at': 1, 'but': 1, 'by': 1, 'en':1, 'with': 1, 'for': 1, 'if': 1, 'in': 1, 'of': 1, 'on': 1, 'the': 1, 'to': 1, 'via': 1 }; // LEAK/CACHE TODO: evaluate using LRU. var TITLE_CASE_CACHE = new Object(); toTitleCase: function (title) { if (!title) return null; var result = TITLE_CASE_CACHE[title]; if (result) { return result; } result = ""; var split = title.toLowerCase().split(" "); for (var i=0; i < split.length; i++) { if (i > 0) { result += " "; } var word = split[i]; if (i == 0 || TITLE_CASE_LOWER_MAP[word] != 1) { word = word.substr(0,1).toUpperCase() + word.substr(1); } result += word; } TITLE_CASE_CACHE[title] = result; return result; },

有一些很好的答案,但是,许多人使用正则表达式来查找单词,但是,由于某种原因,没有人使用正则表达式来替换第一个字符。为了解释,我将提供一个较长的解决方案和一个较短的解决方案。

长期解决方案(更具解释性)。通过使用正则表达式[^\s_\-/]*,我们可以找到句子中的每一个单词。随后,我们可以使用正则表达式。与单词中的第一个字符匹配。使用正则表达式版本的replace来替换这两个函数,我们可以像这样更改解决方案:

function toUpperCase(str){返回str.toUpperCase();} 函数capitalizeWord(word){返回word.replace(/。/,包含);} 函数大写(句子){返回句子。tolowercase()。替换(/ [/ ^ \ s_ \] * / g, capitalizeWord);} console.log(大写(“hello world”));//输出:Hello World

对于做同样事情的单个函数,我们将replace调用嵌套如下:

函数大写(句子){ 返回sentence.toLowerCase()。替换(/[^\s_\-/]*/g, function (word) { 返回word.replace(/。/,函数(ch){返回ch. touppercase ();}); }); } console.log(大写(“hello world”));//输出:Hello World