<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);