The basic of regular expressions

Source: Internet
Author: User

 Today, let's talk about the regular:
Regular full-length regular expressions, also known as regular expressions. English: Regular expression, abbreviated as Reg.
and the regular expression is to deal with the string, set up a series of rules, which is his usage;
Features: Very high performance, convenient.

a regular notation:
1. Pass a parameter
var reg = new RegExp (' String to find () ');
Str.search (reg);
no Return-1 is found, one from left to right, and case-sensitive ;
2. Pass two parameters:
New RegExp (' String to find ', ' G ');
in this, G stands for: global: All;

shorthand for Regular:
var reg =//;

Some methods:
Str.search (reg); search location;
Str.match (reg); Match object;
str.replace (Reg, ' the character to be replaced by '), or: Str.replace (reg,function (s) {});
reg.test (otxt.value): Check if Otxt.value meet demand, return true if required, otherwise return false;

| |: OR;
|: Regular inside OR; (Regular is more lazy, more or less, as long as there is a satisfaction to play true;)
^: The beginning of the line; put in [] inside to represent the exclusion;
$: End of line;

some abbreviations:
I:ignore: Ignore
G:global: Global
M:multi-line: Multiple lines

about escape \:
\d:digital: Digital
\ n: line break;
\s:space: space;
\w:word: words;
\d: Non-digital;
\w: Non-word;
\s: Non-space;
.: any thing;

about []:
[abc]-->a|b|c;
[1-39]-->1 to 3, or 9
[A-z]--->a-z 26 lowercase letters;
[A-z]--->a-z 26 uppercase letters;
[0-9]--->0-9 digital--->\d;
[^0-9]---> Non-digital;--->\d
[^A-Z]---> Non-lowercase letters;
[^a-z]----> non-capital letters;

quantifier: {}
?--->{0,1}, there can be no, there is only one at most.
{n}: exactly n;/\d{3}/: exactly 3 digits;
{n,m} a minimum of n, up to M;
{1,} at least 1, no limit,---> +;
{0,} can be no, there is no limit to the number;----> *;
/ab+/--->abbbbbb;
/(AB) +/--->abababab;
+:n multiple;
Here are some examples of how to share a regular:
1. School check age. html
    <script>       window.onload=function () {           var otxt=document.getelementbyid (' txt ');           Otxt.oninput=function () {               otxt.value=otxt.value.replace (/\d/, ')           }       }    </script>
2. Organize the format. html
    <script>        window.onload=function () {            var Otxt1=document.getelementbyid (' Txt1 ');            var Otxt2=document.getelementbyid (' Txt2 ');            var Obtn=document.getelementbyid (' btn ');            Obtn.onclick=function () {                //otxt2.value=otxt1.value.replace (/^/mg, '    )                otxt2.value= OTxt1.value.replace (/$/mg, ' Qwer ')            }        }    </script>
3. Check Chinese characters. html
    <script>        window.onload=function () {            var otxt=document.getelementbyid (' txt ');            var Obtn=document.getelementbyid (' btn ');            var reg=/^[\u4e00-\u9fa5]{2,6}$/;            Obtn.onclick=function () {                alert (reg.test (Otxt.value)            }}            //var reg=/[^\u4e00-\u9fa5]{2,6}$/*** the range of Chinese characters;        }    </script>
4. Check the landline number. html
    <script>        window.onload= function () {            var otxt=document.getelementbyid (' txt ');            var Obtn=document.getelementbyid (' btn ');            var reg=/^ (0[1-9]\d{1,2}-)? [1-9]\d{6,7}$/;            Obtn.onclick=function () {                alert (reg.test (otxt.value)            }        }}    </script>

5. The input box can only enter numbers. html

    <script>       window.onload=function () {           var otxt=document.getelementbyid (' txt ');           Otxt.oninput=function () {               otxt.value=otxt.value.replace (/\d/, ')           }       }    </script></ Head><body><input type= "text" name= "" id= "TXT"/></body>

6. Filter sensitive words 1.html

<script>    var str= ' I'll be by your side you'll never look back, your every move like a heartbeat affects me all I'll be in your farinose you'll never let go no matter yesterday today and after the end of the day I must come back to do qingfengdian undisturbed once I'll take you to see the everlasting laughter in the sunny days I'm not going to go back to the day every day daily night waiting for the  king called me to patrol the mountain King called me to patrol the mountain I put the car on the five rings Ah Five ring you die four ring more ring AH  Five ring you are more than six ring one day  you will fix to seven ring  fix to seven ring  What do you do more than the five rings two ring when I see you again see if  I see  you again some unspeakable weakness  can only slowly grasp their own sometimes also secretly Tears  sometimes also silently thought oneself also may have the possibility if the heaven arranges in the love and hate season to confuse suddenly too many words some unspeakable weak tenderness suddenly one day you are not in I hear I say '    var reg=/you | too much | one | daily every night | people/g ;    document.write (Str.replace (reg,function (s) {        var tmp= ';        for (var i=0;i< s.length;i++) {            tmp+= ' * ';        }        Return ' <span style= ' color:orangered; > ' +tmp+ ' </span> '    }) </script>
7. Capitalize the first letter. html
<script>        window.onload=function () {            var str= ' Come on baby ';            document.write (Str.replace (/\w+/g,function (s) {               return S.charat (0). toUpperCase () + s.substring (1);            })        }    </script>

  

The basic of regular expressions

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.