使用正则表达式将小写值替换为标题 Case JavaScript



我想使用正则表达式将一串单词中每个单词的小写字母替换为大写字母,即将其更改为标题大小写,以便 str_val="这是有史以来最好的酱汁" 变成"这是有史以来最好的酱汁"。 这是我的代码

function (str_val) {
let reg_exp = /sw/g;
let str_len = str_val.split(' ').length;
while (str_len){
str_val[x].replace(reg_exp, reg_exp.toUpperCase());
x++;
str_len--;
}
return str_val;
}

如何使用正则表达式解决此问题?

在标题大小写中使用以下函数

function title(str) {
return str.replace(/(?:^|s)w/g, function(match) {
return match.toUpperCase();
});
}

最新更新