<span style="font-size:14px;">/*个单词首字母变大写*/
var str="you need to try,dear";//标点属于单词边界
var reg=/\b[a-zA-Z]+\b/ig;
str=str.replace(reg,function(match){
return (match[0].toUpperCase()+match.slice(1)) ;
});
console.log(str);</span>
分组实现首字母大写:
//笔
var str="you need to cry,dear";
var reg=/\b([a-zA-Z])([a-zA-Z]+)\b/ig;
// $1 $2
str=str.replace(reg,function(match,$1,$2){
return $1.toUpperCase()+$2;
});
console.log(str);
本文介绍了一种使用JavaScript实现字符串中每个单词首字母大写的两种方法。一种是通过正则表达式匹配所有单词,另一种是通过分组来实现。
1636

被折叠的 条评论
为什么被折叠?



