What is regular

A re is a rule, a rule for handling strings

Matching of regulars

–reg.test(STR)

Capture of re

Catch the contents of a string that matches the rules –reg.exec(STR)

Create a way

1. Object literals

var reg = /\d/
Copy the code

2. Instance creation method

var reg = new RegExp('\\d');
Copy the code

3. The difference between the two ways

1) Literals are created with metacharacters between two slashes

2) For the requirement of string concatenation, only instance creation can be used

var reg1 = new RegExp('^\\d+'+ name + '\\d+$'.'g');
Copy the code

metacharacters

Characters that have their own meaning between //. Each metacharacter consists of metacharacters and modifiers.

Metacharacters with special meaning

  • \ Escape characters
  • ^ Start with a metacharacter
  • The $ends with a metacharacter
  • \n Matches a newline character
  • Any character except \n
  • () grouping, divide the large re into several small re
  • X | y x or y
  • [xyz] A character in xyz
  • [^xyz] Any character other than xyz
  • [a-z] Any character between a and Z
  • \d Any number between 0 and 9
  • \b Matches a boundary character
  • \w One of a number, letter, or underscore
  • \s Matches a whitespace character (space, TAB, feed)
  • \ D non-numeric
  • \B Non-boundary
  • \W Non-digit, non-letter, non-underscore
  • \S Non-blank character

A quantifier metacharacter representing occurrences

  • ‘*’ 0 – many times
  • ‘+’ 1 – many times
  • ‘? ‘0 and 1
  • {n} n times
  • {n,} n to multiple times
  • {n, m} n – m times

The characteristics of parentheses

1. All characters in brackets represent their own meanings and have no special meanings

var reg2 = $/ / ^ [.];
console.log(reg2.test('1')); //false
console.log(reg2.test('. ')); //true
Copy the code

2. Brackets do not recognize two digits

Increase in captured laziness and cupidity

Lazy: match to the first match on the end

Cupidity: Match the longest result each time

Tackle laziness

G: global

var str = '12345';
var r1 = /\d/;
console.log(str.match(r1)); // ["1", index: 0, input: "12345", groups: undefined]
var r2 = /\d/g;
console.log(str.match(r2)); // ["1", "2", "3", "4", "5"]
Copy the code

Deal with greed

Quantifier metacharacter followed by?

var str = '12345';
var r1 = /\d+/;
console.log(str.match(r1)); // ["12345", index: 0, input: "12345", groups: undefined]
var r2 = /\d+? /;
console.log(str.match(r2)); // ["1", index: 0, input: "12345", groups: undefined]
Copy the code

? The role of

1. After common metacharacters, 0 to 1 occurrences occur

2, after quantifier metacharacters, cancel capture greed

3, in (?,?,?,?,? 🙂 in a group, it is matched but not captured

Group hire

1. Change your priorities

2. Group references

// Note that \2 represents the same content in the second group; \1 means exactly the same content as the first group;
// The value is the same.
var r3 = /^(\w)\1(\w)\2$/;
r3.test('xxll');
Copy the code

3. Group capture. When the re is captured, the large re is matched first and then the small groups are matched

The string replace is used in combination with the re

var s5 = 'z123z345';
// console.log(s5.replace(/(z)\d+/g, 'xl'));
// As with exec, all matches are captured, and all matches are replaced with the target content.
s5.replace(/(z)\d+/g.function () {
    console.log('ok');
    console.log(arguments); // Similar to the result captured by exec
    console.log(RegExp. $1); // Get the contents of the group. Internet Explorer is not compatible
    return 'xl';
})
Copy the code