Application of regular expression conversion case in JS

Source: Internet
Author: User

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
  1. <script type="Text/javascript" >
  2. function Replacereg (reg,str) {
  3. str = Str.tolowercase ();
  4. return Str.replace (Reg,function (m) {return m.touppercase ()})
  5. }
  6. var reg =/\b (\w) |\s (\w)/g;
  7. var str = ' share JavaScript ';
  8. var str2 = ' SHARE JAVASCRIPT ';
  9. var str3 = ' Test \ n str is no good! ';
  10. var str4 = ' final Test ';
  11. document.write (Replacereg (reg,str) +' <br/> ');
  12. document.write (Replacereg (REG,STR2) +' <br/> ');
  13. document.write (Replacereg (REG,STR3) +' <br/> ');
  14. document.write (Replacereg (REG,STR4) +' <br/> ');
  15. </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" >
    1. <script language="JavaScript" >
    2. <!--
    3. var str="Xi nAn shi xUe yuan China people"
    4. Alert (Str.replace (/\s[a-z]/g,function ($) {return $1.tolocaleuppercase ()}). replace (/^[a-
    5. z]/,function ($) {return $1.tolocaleuppercase ()}))
    6. -
    7. </script>

Run the code, directly look at the effect is possible!

Application of regular expression conversion case in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.