preface

RegExp: Regular Expression: is a built-in JS class

  • It’s designed to work with strings
  • Is a rule that tests if a string belongs to the rule (test), and can also capture strings that conform to the rule (exec).
let reg = /\d+/; Console. log(reg.test('12')); // true console.log(reg.test('d12fg')); // true console.log(reg.exec('d12fg')); / / [0: '12',...]Copy the code

Composition of regulars

metacharacters

Regular metacharacters: quantifier metacharacters Special metacharacters ordinary metacharacters

Quantifier metacharacters: *:0 to multiple + : 1 to multiple? : 0 to 1 times {n}: n times {n,}: at least n times {n,m}: n to m timesCopy the code
Special metacharacters (with * are common) \ : Escape characters that convert ordinary metacharacters to special metacharacters, and can convert special metacharacters to ordinary metacharacters.: Any character (except newline) ^: begins with whatnot * $: ends with whatnot * \n: newline character \d: Between 0 and 9 arbitrary number * \ D: not any digital \ w between 0-9: Numbers, letters, underline \ t: tabs \ b: word boundaries \ s: whitespace x | y: take x or y any one of these * [a-z] : A to z of a letter [0-9] + \ d + [a-z] * [a-z, a-z 0-9] : a-z or a-z or any character between 0 and 9 * [@ #] : @ # or any one of the characters [^ a-z] : in addition to the a to z of any character () : group * (? :): matches only, does not capture * (? =): forward check (? !). : Negative pre-checkCopy the code
Common metacharacters: other than special metacharacters such as B n D eCopy the code

The modifier

* let reg = /^f$/ I; * let reg = /^f$/ I;Copy the code

Use of re

Test: Tests whether the current string can be matched with the current re, returning true if the match is successful and false otherwise

1. What does it start with: ^

What ends it with: $

let reg = /18$/; // end with 18 console.log(reg.test('189')); // false console.log(reg.test('198')); // false console.log(reg.test('234567818')); // true -------------------------------------------------------------------------------- let reg = /^18/; // start with 18 console.log(reg.test('189')); // true console.log(reg.test('198')); // false console.log(reg.test('18234567818')); // true -------------------------------------------------------------------------------- let reg = /^18$/; // start with 18 and end with 18 console.log(reg.test('189')); // false console.log(reg.test('198')); // false console.log(reg.test('1845618')); // false console.log(reg.test('1818')); // false console.log(reg.test('18')); // true -------------------------------------------------------------------------------- let reg = /^\d{2}$/; Console. log(reg.test('e2')); // Numbers between 0 and 9 appear twice console.log(reg.test('e2')); // false console.log(reg.test('2e')); // false console.log(reg.test('2e2')); // false console.log(reg.test('22')); // true console.log(reg.test('57')); // true // If you do not add ^ $to the string, the string must match the same character as the regular stringCopy the code

2. Escape character \

Let reg = 2.3 $/ / ^; Console. log(reg.test('2.3')); // Start with 2, middle with any character, end with 3 //. // true console.log(reg.test('2e3')); // true console.log(reg.test('2@3')); // true -------------------------------------------------------------------------------- let reg = /^2\.3$/; // '2.3' // Convert special metacharacters to normal metacharacters console.log(reg.test('2.3')); // true console.log(reg.test('2e3')); // false console.log(reg.test('2@3')); / / false -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / to convert ordinary metacharacters into special metacharacters let reg = /^\d$/ / let STR = 'China '; // Escape characters also work in strings (the first \ converts the second \ into a normal string) console.log(STR); -------------------------------------------------------------------------------- let reg = /^\\d$/; // I just want to match '\d' console.log(reg.test('\\d')); // trueCopy the code

3, x | y: take a between x or y

let reg = /^18|29$/; console.log(reg.test('18')); // true console.log(reg.test('29')); // true console.log(reg.test('189')); // true console.log(reg.test('129')); // true console.log(reg.test('118219')); // false console.log(reg.test('1589')); / / false / / write directly x | y will produce the priority of the mess, let's write that commonly when there will be () to group, Parentheses can change processing priority -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- let 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 console.log(reg.test('1829')); // falseCopy the code

4. Multiple digits are not allowed in [], and brackets are common meanings

let reg = /^[21-68]$/; / / / / (2, 1-6, 8] from 2 or 1-6 or 8 any take a console log (reg. Test (" 22 ")); // false console.log(reg.test('68')); // false console.log(reg.test('2')); // true console.log(reg.test('5')); // true console.log(reg.test('228')); / / false -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / / and brackets are generally common meaning let reg = / ^ [.] $/; Log (reg.test('.')); //. // true console.log(reg.test('1')); // false -------------------------------------------------------------------------------- let reg = /^[\d]$/; // \d is also a special metacharacter console.log(reg.test('6')); // true console.log(reg.test('1')); / / true -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / / / / / year/in a matching id - four digits / / // 2001 1995 1993 1990 let reg = /^[1-2](\d{3})$/; Let reg = /^[1-2]\d{3}$ 01-01-09 10-12 December / / 0 01-09 [1-9] / / 10-12 1 [2-0] let reg = / ^ (0 [1-9] | 1 [2-0]) $/ / / month of matching the let reg1 = / ^ (0/1-9 10 11 | | | 12) matching in $/ / / / / day: 01-31 01-09 10-29 30-31 // 01-09 10-19 20-29 30-31 let reg = /^(0[1-9]|[1-2][0-9]|3[0-1])$/; let reg1 = /^(0[1-9]|1[0-9]|2[0-9]|3[0-1])$/;Copy the code

5, (): Group

/ / 1, improve the matching of priority / / 2, group quoted -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- let STR = 'moon' // 'seew'; 'meeeo / / ([a-z]) \ 1: put just appears in parentheses character to reappear again let reg = / ^ [a-z] ([a-z]) \ [a-z] 1 $/; console.log(reg.test('moon')); / / true -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / for the first time the contents of the packet again let two reg = /^[a-z]([a-z])\1\1[a-z]$/; console.log(reg.test('mooon')); // true -------------------------------------------------------------------------------- let str1 = 'abab'; let reg = /^([a-z][a-z])\1$/; // /^([a-z]{2})\1$/ // /^([a-z])([a-z])\1\2$/; console.log(reg.test('abab')); // trueCopy the code