我想在字符串获得空间时将其字符大写,但不更改其他字母。 例如。
the mango tree -> The Mango Tree
an elephant gone -> An Elephant Gone
the xyz hotel -> The Xyz Hotel
在 JavaScript 中
最佳答案
您可以执行以下操作:
var capitalize = function (text)
{
return text.replace(/\w\S*/g, function (text) {
return text[0].toUpperCase() + text.substring(1);
});
}
alert(capitalize('the dog ran fast'));
与其他建议不同,这将允许您在字符串中保留其他大写字母。例如,字符串“我的变量名称是coolCat”将变为“我的变量名称是CoolCat”。
关于javascript - 每当字符串获得空间时将其字符大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10915138/