Regular expression

Regular expression: the RegExp

Rules for handling strings

  • You can only handle strings
  • It is a rule: you can either verify that a string conforms to a rule (test) or capture the content of a string that conforms to the rule (exec/match…).
let str = "Good good study, day day up!";
//=> Learning regularity is to make rules (including numbers)
let reg = /\d+/;
reg.test(str); //=>false

str = "2019-08-12";
reg.exec(str); //=>["2019",index:0,inputs:" primitive string "]
Copy the code

Writing regular expressions

There are two ways to create it

//=> literal creation method (two slashes are wrapped between each other, which are used to describe the rule's metacharacters)
let reg1 = /\d+/;

//=> Constructor mode creates two arguments: metacharacter string, modifier string
let reg2 = new RegExp("\\d+");
Copy the code

A regular expression consists of two parts

  • metacharacters
  • The modifier
/* Common metacharacter */
//=>1. Set the number of occurrences of quantifier metacharacters* Zero to multiple + one to multiple? Zero or one {n} occurs n times {n,} occurs n to many {n,m} occurs n to m times//=>2. Special metacharacters: a single metacharacter or a combination of metacharacters indicates a special meaning\ escape character (normal -> special -> normal). Any character other than \n (newline) ^ which metacharacter to start with $which metacharacter to end with \n newline \d0~9Between a number \D not0~9A digit between (uppercase and lowercase mean opposite) \w Any character in digits, letters, and underscores \s a whitespace character (including Spaces, tabs, page breaks, etc.) \t a TAB character (a TAB key: Four Spaces) \ b matches a word boundary x | y x or y [xyz] a character in a character in x or y, or z [^ xy] any character other than x/y [a-z] of arbitrary characters [a-z this range specified0-9a-za-z_]===\w [^a-z] ===\w [^a-z] :) only matches but does not capture (? =) forward check (? !). Negative lessonsCopy the code
/* Regular expression modifiers: img*/I =>ignoreCase ignores word case matching m =>multiline can match multiple lines g =>globalThe global matching/* /A/.test('lalala') =>false /A/i.test('lalala') =>true */
Copy the code

Metacharacter detailed parsing

^ $

let reg = /^\d/;
console.log(reg.test("aaa")); //=>false
console.log(reg.test("2019aaa"));//=>true
console.log(reg.test("aaa2019"));//=>false
Copy the code
let reg = /\d$/;
console.log(reg.test("aaa")); //=>false
console.log(reg.test("2019aaa"));//=>false
console.log(reg.test("aaa2019"));//=>true
Copy the code
//=>^/$Do not add either: The string contains the content that complies with the rules
let reg1 = /\d+/;
//=>^/$Add both: the string must be consistent with the rule
let reg2 = /^\d+$/;

//=> Example: verify mobile phone number (11 digits, the first digit is 1)
let reg = /^1\d{10}$/;
Copy the code

\

//=>. Is not a decimal point, is any character except \n
let reg = 2.3 $/ / ^;
console.log(reg.test("2.3"));//=>true
console.log(reg.test("2 @ 3"));//=>true
console.log(reg.test("23"));//=>false

//=> Based on the escape character, so that it can only represent the decimal point
reg = $/ / ^ 2 \. 3;
console.log(reg.test("2.3"));//=>true
console.log(reg.test("2 @ 3"));//=>false

let str = "\\d";
reg = /^\d$/; //=>\d represents the numbers from 0 to 9
console.log(reg.test(str)); //=>false
reg = /^\\d$/; //=> Convert special coincidence to normal
console.log(reg.test(str)); //=>true
Copy the code

x|y

let reg = 18 | $29 / / ^;
console.log(reg.test("18")); //=>true
console.log(reg.test("29")); //=>true
console.log(reg.test("129")); //=>true
console.log(reg.test("189")); //=>true
console.log(reg.test("1829")); //=>true
console.log(reg.test("829")); //=>true
console.log(reg.test("182")); //=>true
/ / -- - direct x | y will mess priority problems, normally we write is accompanied by grouping parentheses, because parentheses change processing priority = > parentheses: grouping
reg = $/ / ^ 18 | (29);
console.log(reg.test("18")); //=>true
console.log(reg.test("29")); //=>true
console.log(reg.test("129")); //=>false
console.log(reg.test("189")); //=>false
//=> Can only be 18 or 29
Copy the code

[]

//1. The characters in brackets generally represent their own meanings
let reg = / ^ @ + $/;
console.log(reg.test("@")); //=>true
console.log(reg.test("+")); //=>true
console.log(reg.test("@ @")); //=>false
console.log(reg.test("@ +")); //=>false

reg = /^[\d]$/; //=>\d in brackets is still 0-9
console.log(reg.test("d"));//=>false
console.log(reg.test("\ \"));//=>false
console.log(reg.test("9"));//=>true

//2. There are no multiple digits in brackets
reg = $/ / ^ [18];
console.log(reg.test("1")); //=>true
console.log(reg.test("8")); //=>true
console.log(reg.test("18")); //=>false

reg = $/ / ^ [10-29]; //=>1 or 0-2 or 9
console.log(reg.test("1"));//=>true
console.log(reg.test("9"));//=>true
console.log(reg.test("0"));//=>true
console.log(reg.test("2"));//=>true
console.log(reg.test("10"));//=>false
Copy the code

Common regular expressions

  1. Verifies whether it is a significant number

    /* * Rule analysis * 1. The +- sign may or may not occur. * 2. A 0-9 can, more than the first can't is 0 (\ d | (1-9] [\ d +)) * 3. May or may not have a decimal part, but must be followed by a decimal point + number (\.\d+)? * /
    let reg = / ^ (+ -)? (\d|([1-9]\d+))(\.\d+)? $/;
    Copy the code
  2. Verify password

    //=> Digits, letters, and underscores (_)
    / / = > 6 ~ 16
    let val = userPassInp.value,
        reg = 16th {6} $/ ^ \ w /;
    let flag=reg.test(val);
    / * function checkPass (val) {if (val. Length < 6 | | val. The length > 16) {alert (' length must be between 6 and 16! '); return; } let area=['a','b'....'_']; //=> Contains digits, letters, and underscores. For (let I =0; i
    Copy the code
  3. Verify real name

    /^[u4E00-\u9FA5]$/ * 2 Name length 2 ~ 10 * 3. There may be translation, the Chinese character (· [\ u4E00 - \ u9FA5] {2, 10})} {0, 2 * /
    let reg = / ^ [\ u4E00 - \ u9FA5] {2, 10} (· [\ u4E00 - \ u9FA5] {2, 10})} {0, 2 $/;
    Copy the code
  4. Authenticating mailbox

    let reg = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    
    //=> \w+((-\w+)|(\.\w+))*
    //1. Start with alphanumeric underscore (1 to multiple digits)
    //2. Can also be - alphanumeric underscore or. Alphanumeric underscore, overall zero to multiple times
    //=> The mailbox name can contain digits, letters, underscores (_), hyphens (-), and. Several components, but -/. Does not occur consecutively or as a starting point
    
    //=> @[A-Za-z0-9]+
    //1.@ followed by: number, letter (1-bit)
    
    //=> ((\.|-)[A-Za-z0-9]+)*
    //1. Add the name after @
    // multiple domains
    // Enterprise email [email protected]
    
    //=> \.[A-Za-z0-9]+
    / / 1. The match is the final domain name (com/.cn/.org/.edu/.net.)
    Copy the code
  5. Id Card Number

    * * * * * * * * * * * * * * * * * * * * * * * * * * * The last digit => X or number * the penultimate digit => even female odd male * The rest is calculated by the algorithm */
    //let reg = /^\d{17}(\d|X)$/;
    //=> The second function of the bracketing group: group capture, not only can capture the information of the large re match, but also can capture the content of each small group separately
    let reg = /^(\d{6})(\d{4})(\d{2})(\d{2})\d{2}(\d)(\d|X)$/;
    reg.exec("130828199912040677"); / / = > [" 130828199912040677 ", "130828", "1999", "12", "04", "1", "7"...]. The result of the capture is an array containing the contents of each small group individually retrieved
    Copy the code