Example of using a regular expression in JavaScript to convert the case sensitivity. The regular expression is case sensitive.

Source: Internet
Author: User

Example of using a regular expression in JavaScript to convert the case sensitivity. The regular expression is case sensitive.

Use regular expressions in js to convert the case sensitivity. The code is very simple. See the Code:

Uppercase letters and lowercase letters

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

Only the first letter is in upper case. Other letters are not in upper case.

<script language="JavaScript"> <!-- var str="xi nAn shi you xUe yuan china people" alert(str.replace(/\s[a-z]/g,function($1){return $1.toLocaleUpperCase()}).replace(/^[a- z]/,function($1){return $1.toLocaleUpperCase()})) //--> </script>

Run the code and check the effect!


Case-insensitive conversion of a JAVA Regular Expression

. ReplaceAll ("[^ a-zA-Z] + ","");
You mean, remove all the non-letters in the string, and leave only the letters for conversion. That's okay. I just tried it.

The regular expression is case insensitive.

P = Pattern. compile ("^" + key + "= (. *) $", Pattern. CASE_INSENSITIVE );

Add Pattern. CASE_INSENSITIVE to ignore case sensitivity.
Reference: www.zlovezl.cn

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.