有没有一种简单的方法将字符串转换为标题格式?例如,john smith
变成 John Smith
。我不想要像John Resig的解决方案那样复杂的东西,只是(希望)一种一两行代码就能实现的方法。
有没有一种简单的方法将字符串转换为标题格式?例如,john smith
变成 John Smith
。我不想要像John Resig的解决方案那样复杂的东西,只是(希望)一种一两行代码就能实现的方法。
"SOFÍA vergara".toLowerCase().replace(/\b(\s\w|^\w)/g, function (txt) { return txt.toUpperCase(); });
将单词转换为标题大小写的简单方法
使用“切片”方法和字符串连接
str.slice(0, 1).toUpperCase() + str.slice(1, str.length)
*如果要将单词其余部分转换为小写,请在结尾添加.toLowerCase()
使用ES6 Spread Operator、Map和Join
[...str].map((w, i) => i === 0 ? w[0].toUpperCase() : w).join('')
我的一行代码解决方案:
String.prototype.capitalizeWords = function() {
return this.split(" ").map(function(ele){ return ele[0].toUpperCase() + ele.slice(1).toLowerCase();}).join(" ");
};
capitalizeWords()
方法。例如:var myS = "this actually works!";
myS.capitalizeWords();
>>> This Actually Works
我的另一种解决方案:
function capitalizeFirstLetter(word) {
return word[0].toUpperCase() + word.slice(1).toLowerCase();
}
String.prototype.capitalizeAllWords = function() {
var arr = this.split(" ");
for(var i = 0; i < arr.length; i++) {
arr[i] = capitalizeFirstLetter(arr[i]);
}
return arr.join(" ");
};
然后,您可以在任何字符串上调用capitalizeWords()
方法。例如:
var myStr = "this one works too!";
myStr.capitalizeWords();
>>> This One Works Too
基于 Greg Dean 答案的另一种解决方案:
function capitalizeFirstLetter(word) {
return word[0].toUpperCase() + word.slice(1).toLowerCase();
}
String.prototype.capitalizeWords = function() {
return this.replace(/\w\S*/g, capitalizeFirstLetter);
};
capitalizeWords()
方法。例如:var myString = "yes and no";
myString.capitalizeWords()
>>> Yes And No
function titleCase(str) {
return str.toLowerCase().split(' ').map(function(val) { return val.replace(val[0], val[0].toUpperCase()); }).join(' ');
}
let str = 'john smith';
let res = str.split(" ");
res.forEach((w, index) => {
res[index] = w.charAt(0).toUpperCase().concat(w.slice(1, w.length))
});
res = res.join(" ");
console.log(res);
const textString = "Convert string to title case with Javascript.";
const converted = textString.replace(/\b[a-zA-Z]/g, (match) => match.toUpperCase());
console.log(converted)
var string = "tEsT"
string = string.toLowerCase()
var output= string.charAt(0).toUpperCase() + string.slice(1)
console.log(output)
alert(output)
var string = "tEsT"
string = string.toLowerCase()
string.charAt(0).toUpperCase() + string.slice(1)
function camelCase(str) {
return str.replace(/((?:^|\.)\w|\b(?!(?:a|amid|an|and|anti|as|at|but|but|by|by|down|for|for|for|from|from|in|into|like|near|nor|of|of|off|on|on|onto|or|over|past|per|plus|save|so|than|the|to|to|up|upon|via|with|without|yet)\b)\w)/g, function(character) {
return character.toUpperCase();
})}
console.log(camelCase('The quick brown fox jumped over the lazy dog, named butter, who was taking a nap outside the u.s. Post Office. The fox jumped so high that NASA saw him on their radar.'));
toTitleCase()
方法,使其像“原生”的 toLowerCase()
一样适用于 str.toTitleCase()
,你会怎么做? - Razvan Zamfirprototype
。你可以这样做 String.prototype.toTitleCase = function() {...}
https://www.codementor.io/@dhruvkumarjha/extending-the-javascript-string-prototype-f3o5xjia6 - Regular Jo大家好,这里是我的答案。如果您的问题得到解决,请评论并点赞。
function toTitleCase(str) {
return str.replace(
/(\w*\W*|\w*)\s*/g,
function(txt) {
return(txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase())
}
);
}
<form>
Input:
<br /><textarea name="input" onchange="form.output.value=toTitleCase(this.value)" onkeyup="form.output.value=toTitleCase(this.value)"></textarea>
<br />Output:
<br /><textarea name="output" readonly onclick="select(this)"></textarea>
</form>