JS应用正则表达式转换大小写示例
js中应用正则表达式转换大小写,代码很简单,看代码:
以下首字母大写,其它字母小写
<scripttype="text/javascript">
functionreplaceReg(reg,str){
str=str.toLowerCase();
returnstr.replace(reg,function(m){returnm.toUpperCase()})
}
varreg=/\b(\w)|\s(\w)/g;
varstr='sharejavascript';
varstr2='SHAREJAVASCRIPT';
varstr3='Test\nstrisnogood!';
varstr4='finaltest';
document.write(replaceReg(reg,str)+'<br/>');
document.write(replaceReg(reg,str2)+'<br/>');
document.write(replaceReg(reg,str3)+'<br/>');
document.write(replaceReg(reg,str4)+'<br/>');
</script>
以下只首字母大写,其它字母大小写不作处理
<scriptlanguage="JavaScript">
<!--
varstr="xinAnshiyouxUeyuanchinapeople"
alert(str.replace(/\s[a-z]/g,function($1){return$1.toLocaleUpperCase()}).replace(/^[a-
z]/,function($1){return$1.toLocaleUpperCase()}))
//-->
</script>
运行代码,直接看效果就可以了!
热门推荐
10 周末祝福语简短的英语
11 天气转凉祝福语简短
12 比较简短浪漫的祝福语
13 端午发给客户祝福语简短
14 祝福语大全简短赞美同学
15 除夕的拜年祝福语简短
16 春节后的祝福语简短
17 钱包祝福语简短10字
18 圣诞祝福语简短小学