JS in the application of the regular expression conversion case, the code is very simple, see the code:
The following initials are capitalized, the other letters are lowercase
[JavaScript]View Plaincopy
- <script type="Text/javascript" >
- function Replacereg (reg,str) {
- str = Str.tolowercase ();
- return Str.replace (Reg,function (m) {return m.touppercase ()})
- }
- var reg =/\b (\w) |\s (\w)/g;
- var str = ' share JavaScript ';
- var str2 = ' SHARE JAVASCRIPT ';
- var str3 = ' Test \ n str is no good! ';
- var str4 = ' final Test ';
- 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>
==============================================================
The following initials are capitalized, and the other letters are not handled in any case.
[JavaScript]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- <script language="JavaScript" >
- <!--
- var str="Xi nAn shi xUe yuan China people"
- Alert (Str.replace (/\s[a-z]/g,function ($) {return $1.tolocaleuppercase ()}). replace (/^[a-
- z]/,function ($) {return $1.tolocaleuppercase ()}))
- -
- </script>
Run the code, directly look at the effect is possible!
Application of regular expression conversion case in JS