Match the pattern

  1. Matching capture groups (XXX) Parentheses contain separate groups (in characters) inside matching parentheses
  2. Non-capture grouping (? : XXX) is preceded by? : Does not group matches in parentheses (accounts for characters)
  3. Positive positive foresight (? = XXX) is preceded by? = Looking backwards must conform to rules (no characters)
  4. Positive negative after (? ! XXX) is preceded by? ! Looking backwards must not conform to rules (not characters)
  5. Reverse positive forward looking (? <= XXX) is preceded by? <= Look ahead must conform to rules (not characters)
  6. Negative backwardness (?

Matching capture group

  1. Matching capture groups (XXX) Parentheses contain separate groups (in characters) inside matching parentheses
console.log('1ab'.match(/1([a-z])([a-z])/)) 
// [ '1ab', 'a', 'b', index: 0, input: '1ab', groups: undefined ]
Copy the code

Matching capture group

  1. Non-capture grouping (? : XXX) is preceded by? : Does not group matches in parentheses (accounts for characters)
console.log('1ab'.match(/ 1 (? :[a-z])([a-z])/)) 
// [ '1ab', 'b', index: 0, input: '1ab', groups: undefined ]
console.log('1ab'.match(/1[a-z]([a-z])/)) 
// [ '1ab', 'b', index: 0, input: '1ab', groups: undefined ]
Copy the code

Positive positive forward-looking

  1. Positive positive foresight (? = XXX) is preceded by? (1 must be followed by a lowercase letter and does not occupy a character. Continue to match a)
console.log('1a'.match(/ 1 (? =[a-z])([a-z])/)) 
// [ '1a', 'a', index: 0, input: '1a', groups: undefined ]
console.log('1a'.match(/1([a-z])([a-z])/)) // null
Copy the code

Positive negative backward looking

  1. Positive negative after (? ! XXX) is preceded by? ! Must not match the rule (does not occupy the character) (1 is not followed by a capital letter, does not occupy the character, continues to match a)
console.log('1a'.match(/ 1 (? ! [A-Z])([a-z])/)) 
// [ '1a', 'a', index: 0, input: '1a', groups: undefined ]
Copy the code

Reverse positive forward looking

  1. Reverse positive forward looking (? <= XXX) is preceded by? <= looking ahead must comply with the rule (no characters) (1 must be preceded by a capital letter, no characters)
console.log('1a'.match(/ (? <=[A-Z])1([a-z])/)) // null
console.log('A1a'.match(/ (? <=[A-Z])1([a-z])/)) 
//[ '1a', 'a', index: 1, input: 'A1a', groups: undefined ]
console.log('A1a'.match(/[A-Z](? <=[A-Z])1([a-z])/)) 
//[ 'A1a', 'a', index: 1, input: 'A1a', groups: undefined ]
Copy the code

Reverse negative backtracking

  1. Negative backwardness (?
console.log('1a'.match(/ (? 
      )) 
// [ '1a', 'a', index: 0, input: '1a', groups: undefined ]
Copy the code

Greed mode

  1. Greedy mode matches the most matched characters by default
  2. Non-greedy mode quantifier added after? Matches the least number of characters

Greedy matches multiple (default), non-greedy matches least

console.log('aaa'.match(/[a-z]+/)) // [ 'aaa', index: 0, input: 'aaa', groups: undefined ]
// The least greedy pattern matches
console.log('aaa'.match(/[a-z]+? /)) // [ 'a', index: 0, input: 'aaa', groups: undefined ]
Copy the code