将数组中每个单词的首字母大写
Capitalize first letters of every word in array
我有一个数组
var my_array= [ "color - black", "color - light blue", "color - Red" ]
我想用":"替换" - "并将该数组中每个单词的首字母大写:
var my_array= [ "Color: Black", "Color:Light Blue", "Color: Red" ]
我试试这个
String.prototype.capitalize = function() {
return this.replace(/(?:^|'s)'S/g, function(a) { return a.toUpperCase(); });
};
for(var i=0; i < my_array.length; i++) {
my_array[i] = my_array[i].capitalize().replace(/ -/g, ":");
}
但它仅适用于
[ "Color: Black", "Color: Red" ]
我得到
[ "Color:Light: Blue" ]
带两个" : "
但我想得到
[ "Color:Light Blue" ]
var my_array= [ "color - black", "color - light blue", "color - Red" ]
String.prototype.capitalize = function() {
return this.replace(/(?:^|'s)'S/g, function(a) { return a.toUpperCase(); });
};
for(var i=0; i < my_array.length; i++) {
my_array[i] = my_array[i].capitalize().replace(/ -/g, ":");
}
document.write('<pre>'+JSON.stringify(my_array));
在将"-"替换为":"后尝试此操作
String.prototype.capitalize = function () {
return this.replace(/'w'S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
创建数组或打印数组时使用大写
function capitalizeFirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
要替换,请阅读
http://www.w3schools.com/jsref/jsref_replace.asp
相关文章:
- Javascript-从随机的单词数组中创建段落
- 将组成单词的字符添加到数组的开头
- undercore返回数组中对象的indes,其中单词存在于对象中的句子中
- Javascript-在一个数组中搜索整个单词,并创建一个符合条件的新数组
- 在两个位置显示数组中的随机单词
- 如果输入包含数组中的任何单词
- 单词数组到字符串
- Javascript/Node AI-如何使用match从单词数组中重复单词
- 在单词数组中查找相似的单词
- 在单词数组中搜索单词
- 将句子字符串拆分为单词数组,然后将单词数组拆分为单词数组中的字符数组
- underscore.js基于单词数组从字符串中删除文本
- 从JavaScript中的单词数组中查找第n个字符
- Coffeescription在页面上的单词数组中进行迭代
- 我们如何用单词数组填充一组
- 用正则表达式连续收集单词数组中的任意单词
- 试图响应从node.js http服务器中分割成单词数组的文本
- 在mark.js中为单独的单词数组设置样式
- 将字符串拆分为单词数组以查找“最长”
- 王牌编辑器查找全部不起作用.需要所有带有 dif 的单词.数组中的值