Regular expressions are a set of independent rules that almost every programming language provides classes that implement these rules
1. Create a regular object in two ways
The first method of creation:
var reg = new RegExp (pattern,flags);
Second method of Creation (recommended):
var reg =/pattern/flags;//flags If is g, indicates global match, if I, the case is ignored
2. Two common methods
Method |
Role |
Test () |
Tests if a piece of text matches this regular expression Retrieves the specified value in a string. The return value is true or false. |
EXEC () |
You can get a portion of text that matches a regular match (the return value is an array or null) The return value is the value that was found. If no match is found, NULL is returned. |
3. Example
(1) Identify each matching item
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/86/9F/wKioL1fFpR_x78CIAAGpJMuVDgM508.gif "title=" Find each matching item. gif "alt=" Wkiol1ffpr_x78ciaagpjmuvdgm508.gif "/>
(2) Find out all mobile phone numbers
<! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/A0/wKiom1fFpb-R0TwVAAHH9SgaYZc680.gif "title=" Find out all mobile phone numbers. gif "alt=" Wkiom1ffpb-r0twvaahh9sgayzc680.gif "/>
(3) Replace All
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/86/9F/wKioL1fFpmqyd9i_AAFh8K02vTg533.gif "title=" Replace all. gif "alt=" Wkiol1ffpmqyd9i_aafh8k02vtg533.gif "/>
(4) Verifying the mailbox format
<! doctype html public "-//w3c//dtd html 4.01 transitional//en" "HTTP// Www.w3.org/TR/html4/loose.dtd ">650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/A0/wKiom1fFqOuB8utEAADQMPHGq-4305.gif "title=" Verify the mailbox format 2.gif "alt=" Wkiom1ffqoub8uteaadqmphgq-4305.gif "/>
The regexp in JavaScript