Regular expression:
1, what is RegExp? RegExp is the abbreviation for regular expressions. The RegExp object is used to specify what is retrieved in the text.
2, the definition of Regexp:var + variable name =new RegExp ();
3, the RegExp object has 3 methods:
1 Test () retrieves the specified value in the string, and the return value is true or false.
Copy Code code as follows:
var p1=new Reg ("E");
document.write (Reg.test ("Welcome to china!"));
2) EXEC ()
Copy Code code as follows:
var p1=new Reg ("E");
document.write (Reg.exec ("Welcome to china!"));
3) Compile ()
Copy Code code as follows:
var p1=new Reg ("E");
document.write (Reg.compile ("Welcome to china!"));
4, modifier
1) I perform an insensitive match for case sensitivity
2) G perform global match
3) m performs multiple line matching
5, square brackets (used to find a range of characters)
1) [ABC] find any character between square brackets
2) [^ABC] finds any characters that are not between brackets
3) [0-9] Find the number between any 0-9
4) [A-z] find characters between any lowercase a-Z
5) [A-z] find any characters between uppercase A-Z
6) [A-z] find any character between uppercase A and lowercase Z
7) [ADGK] finds any character within a given set
8) [^ADGK] finds any character outside a given set
9) (Red|biue|green) to find any given option
6, meta characters
1) \w: Find word characters
2) \w: Find non-word characters
3) \d: Find numbers
4) \d: Find non-numeric characters
7. Measure words
n+ matches any string that contains at least one n.
n matches any string containing 0 or more n.
N? matches any string that contains 0 or one n.
N{x} matches a string that contains a sequence of X N.
N{x,y} matches a string containing a sequence of X or Y N.
N{x, a string that matches a sequence containing at least X N.
n$ matches any string ending with N.
^n matches any string that starts with N.
? =n matches any string immediately followed by the specified string n.
?! n matches any string that is not followed by the specified string n.
"Exercise 1" to determine whether the input ID card format is correct
Copy Code code as follows:
<body>
<label> Please enter your ID number:</label>
<input name= "" type= "text" id= "Shenfen"/>
<input name= "" type= "Submit" id= "Textfi" value= "Submit 1" onclick= "Test ()"/><!--submit button, triggering function-->
</body>
<!--determine whether the ID card is entered correctly-->
<script>
function test ()
{
var reg=new RegExp (/^[0-9]{17}[0-9| x]$/);<!--The format of the regular expression: begins with/^, ends with $/, and the number and letter ranges are in brackets-->
var Text=document.getelementbyid ("Shenfen") .value;<!--the ID of the string that entered the text box corresponds to the ID of the text box-->
Judge whether the area is legal (with If...else)
if (reg.test (text))
{
Alert ("Input is correct")
}
Else
{
Alert ("Input error");
}
}
</script>
Effect Chart:
"Exercise 2" Determines whether the entered mailbox is in the correct format
Copy Code code as follows:
<body>
<label> Please enter your email:</label>
<input name= "" type= "text" id= "Youxiang"/>
<input name= "" type= "Submit" value= "Submit 2" onclick= "Y ()"/>
</body>
<script>
function y ()
{
var yx=new RegExp (/^[0-9| a-z|_]{1,17}@[a-z|0-9]{1,5}. (COM|CN|NET|ORG|CC) $/);
var Shuru=document.getelementbyid ("Youxiang"). Value; <!--defines a variable, calls the entire function, and must write the variable within the function-->
if (Yx.test (Shuru)) <!--test Regular expression Method-->
{
Alert ("Zhengque");
}
Else
{
Alert ("Cuowu");
}
}
</script>
Effect Chart:
Relatively speaking, JS is a very important point of expression, many places need to cooperate with the only line, so the small partners must learn this piece of content.