Regular expression:
1. What is RegExp? RegExp is the abbreviation of regular expressions. The RegExp object is used to specify the content retrieved in text.
2. Define RegExp: var + variable name = new RegExp();
3. There are 3 methods for the RegExp object:
1) test() retrieves the specified value in the string, and the return value is true or false.
The code copy is as follows:
var p1=new Reg("e");
document.write(Reg.test("welcome to China!"));
2) exec()
The code copy is as follows:
var p1=new Reg("e");
document.write(Reg.exec("welcome to China!"));
3) compile()
The code copy is as follows:
var p1=new Reg("e");
document.write(Reg.compile("welcome to China!"));
4. Modifier
1) i perform case-insensitive matching
2) g perform global matching
3) m Perform multi-line matching
5. Square brackets (used to find characters in a certain range)
1) [abc] Find any character between square brackets
2) [^abc] Find any characters that are not between square brackets
3) [0-9] Find any number between 0-9
4) [az] Find characters between any lowercase az
5) [AZ] Find any characters between capital AZ
6) [Az] Find any characters between uppercase A-lowercase z
7) [adgk] Find any characters in a given set
8) [^adgk] Find any character outside the given set
9) (red|biue|green) Find any given option
6. Metacharacter
1)/w: Find word characters
2)/W: Find non-word characters
3)/d: Find numbers
4)/D: Find non-numeric characters
7. Quantitative words
n+ matches any string containing at least one n.
n* matches any string containing zero or more ns.
n? Match any string containing zero or one n.
n{X} matches a string containing X sequences of n.
n{X,Y} matches a string containing sequences of X or Y n.
n{X,} matches a string containing at least X n sequences.
n$ matches any string with the ending n.
^n matches any string that starts with n.
?=n matches any string followed by the specified string n.
?!n matches any string that is not immediately followed by the specified string n.
【Exercise Question 1】Direction whether the input ID card format is correct
The code copy is 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, trigger function-->
</body>
</html>
<!--Judge whether the ID card is entered correctly->
<script>
function test()
{
var reg=new RegExp(/^[0-9]{17}[0-9|X]$/);<!--Format of regular expression: start with /^ and end with $/, and numbers and letter ranges are in brackets-->
var text=document.getElementById("shenfen").value;<!--The id of the string in the input text box corresponds to the id of the text box-->
//Judge whether the region is legal (use if...else)
if(reg.test(text))
{
alert("Input Correct")
}
else
{
alert("Input Error");
}
}
</script>
Reproduction image:
【Exercise Question 2】Judge whether the entered email format is correct
The code copy is as follows:
<body>
<label>Please enter your email address:</label>
<input name="" type="text" id="youxiang"/>
<input name="" type="submit" value="submit 2" onclick="y()" />
</body>
</html>
<script>
function y()
{
var yx=new RegExp(/^[0-9|Az|_]{1,17}@[Az|0-9]{1,5}.(com|cn|net|org|cc)$/);
var shuru=document.getElementById("youxiang").value; <!--Define the variable, and the entire function is called, the variable must be written in the function-->
if(yx.test(shuru))<!-- Method of test regular expression-->
{
alert("zhengque");
}
else
{
alert("cuowu");
}
}
</script>
Reproduction image:
Relatively speaking, regular expressions in js are a very important point. Many places need to cooperate with regular expressions, so friends must learn this content well.