What is re?

When writing front-end forms, we usually need to define some rules when we encounter fields such as mobile phone number, phone number, email address and ID card number that require certain format verification. At this time, we need to use regular expressions. 1 provides a class in JS: RegExp Regular class 2 is not a basic type. A special object with a useful API like Date has a complex syntax. A powerful and commonly used text processing tool

Re creation: constructor creation

 new RegExp(character rule, permission modifier);// Permission modifiers: I ignore case and g global match m multi-line match
 var r1 = new RegExp('hello [/ d] {2, 4} world',);
 var r1 = new RegExp('the rules'.'Modifier');
 var r2 = / rules /The modifier;Copy the code

Understand what Regular is up to three soul tortures?

1. Match what? 2. Match is not what 3. Match how many times

What match?

 // For example, if you want to match character A, you can write /a/ as long as the string position is a
    var a = /a/
    console.log(a.test("javascript"))
    console.log(/a/.test("javascript"))

    // The metacharacter ^ (start identifier) /^a/;
    console.log(/^a/.test("javascrittt"))   // Returns false instead of starting with a
    console.log(/^a/.test("abc"))           // Returns true starting with a

    // Matches a string ending in ah plus the metacharacter $(end position identifier) /a$/
    console.log(/a$/.test("javascript"))    // Return false if it does not end in a
    console.log(/a$/.test("cba"))           // return true with ah

    / / character a or b can put the corresponding character in the brackets [] a | b/as long as the string contains a or b can match
    console.log(/[a|b]/.test("byte"))       //true

    / / match strings ABC or xyz/ABC | xyz /
    console.log(/abc|xyz/.test("xyzasdas"))    // This string contains xyz so return true
Copy the code
Match what ahead (look ahead)?
// Match what precedes (preview)
//exp1(? =exp2) Match EXP2. The matching result of EXP1 does not contain EXP2
// For example, match the part of the string script before Java/Java (? =script)/
console.log(/java(? =script)/.test("javascript,javaee,typescript")) // The javascript in the string complies with the rule returns true
//1 Use the exec method to verify the matching result
console.log(/java(? =script)/.exec("javascript,javaee,typescript")) //["java", index: 0, input: "javascript,javaee,typescript", groups: undefined]
Copy the code
Match the back of what?
/ / (? <=exp2) Exp1 matches exp2
// Match script/Java (? < = 22) /
console.log(/ (? <=java)ee/.test("javaee"))       // True is returned when javaEE meets the rule in the string
Copy the code
Intercepts data before or after a specific character in a string
    1* Intercept the data between the strings a and f/ a(\S*)f/2* After getting the specified string (? <= Specifies a string.3* Before getting the specified string (? = specified string)Copy the code

What is a match not?

A /[^a]/ */
    console.log(/[^a]/.test("aaaaaa"))      // String is all a false
    console.log(/[^a]/.test("aaaaaa"))      // the character is not a returns true
    
    // Match any type that does not start with a with the metacharacter ^ /^[^a]/
    console.log(/^[^a]/.test("javascript"))   
    console.log(/^[^a]/.test("javascript"))   
Copy the code

Case study

 The //.match() method retrieves a specified value within a string or finds a match for one or more regular expressions
    var str = "http://zhipur.com/item?data=SN34567892143423423";
    var code1 = str.match(/ \? data=(.*)/) [1]    / / take? Data = all strings that follow
    var code2 = str.match(/data=(.*)/) [1]      // Take all strings following data=
    var code3 = str.match(/data=(.*)/) [0]      // Take the string containing data= and the following
    var code4 = str.match(/(\S*)? data=/) [1]
    var code5 = str.match(/(\S*)? data=/) [0]
    console.log(code1)                         //SN34567892143423423 
    console.log(code2)                         //SN34567892143423423 
    console.log(code3)                         //data=SN34567892143423423   
    console.log(code4)                         //http://zhipur.com/item? 
    console.log(code5)                         //http://zhipur.com/item?data= 
Copy the code

The interview questions

Example (Operating URL links) Components of a common URL 1 Protocol Part Protocol part of the URL HTTP protocol used in the PROTOCOL part of the URL Various protocols can be used on the Internet, such as HTTP FTP. The // following HTTP is delimiter 2 Domain name part Domain name part of the URL www.aspxfans.com a url can also use an IP address as a domain name using the 3 port part followed by the domain name is the port between the domain name and the port: As a separator port is not a required part of a URL. If omitted, port 80 will be used by default. 4 Virtual directory section starting from the first/after the domain name to the last/is the virtual directory part From the last/after the domain name to? So far is the file name part if not? Start from the last/to # if not? Asp. The file name is not a required part of the URL. If omitted, the default file name is used. 6 the anchor part is the anchor part from # to the end Parameter part from? BoadrdID =5&ID=2461&page=1 In the query part, multiple parameters are separated by ampersand (&)