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 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语