This is the 11th day of my participation in the August Wenwen Challenge.More challenges in August

Preface: What is regular?

1. A regular expression is a string search pattern that uses a single string to describe and match a series of strings that conform to a certain syntactic rule.

2. Search mode can be used for text search and text replacement.

3. A regular expression is a search pattern formed by a sequence of characters.

4. When you search for data in text, you can use search mode to describe what you are looking for.

5. A regular expression can be a simple character or a more complex pattern.

6. Regular expressions can be used for all text search and text replacement operations.

How to create regular expressions

var reg = /pattern/flags
// how literals are created
var reg = new RegExp(pattern,flags);
// Instance creation modePattern: indicates a regular expressionflags: Identifier (modifier) The identifier mainly includes:1.I ignores case matching2.M multi-line matching, that is, items in the next line that continue to match the re when the end of the line of text is reached3.G The global matching pattern applies to all strings, rather than stopping when the first match is foundCopy the code

The difference between how literals are created and how constructors are created

  1. Literal creation cannot concatenate strings, instance creation can

var regParam = 'cm';
var reg1 = new RegExp(regParam+'1');
var reg2 = /regParam/;
console.log(reg1); // /cm1/
console.log(reg2); // /regParam/

Copy the code
  1. Characters with special meaning in literal creation do not need to be escaped. Instance creation needs to be escaped

var reg1 = new RegExp('\d'); // /d/
var reg2 = new RegExp('\\d') // /\d/
var reg3 = /\d/; // /\d/

Copy the code


A metacharacter representing a special meaning

\d : 0-9Any digit \d in between takes only one position \w: digit, letter, underscore0-9A-z a-z _ \ s: such as Spaces or blank \ D: in addition to the \ \ D W: in addition to the \ W \ s: besides \ s. : besides \ n any one character \ : escape character | : or () : group \ n: match a newline \ b: Matches both the beginning and end Spaces of the boundary string as bounds => does not occupy the number of string digits ^ : qualifying the beginning position => itself does not occupy the position$[A-Z] : indicates that any letter [] can be used. [^ A-z] : indicates that no letter [] can be used. [^ A-z] : indicates that no letter [] can be usedCopy the code

A quantifier metacharacter representing a number of times

* : 0To multiple + :1To more than one? :0Time or1{n} : exactly n times; {n,} : n to multiple {n,m} : n to m timesCopy the code

Characteristic of regularity

  • TanLanXing

    Greedy is when the re tries to capture as many items as possible at a time. If we want to capture as few qualified strings as possible, we can add? To the quantifier metacharacter.

  • Lazy sex

    Laziness is when the re is successfully captured once, regardless of whether the following string matches the condition. If we want to capture all eligible strings in the target, we can use the g identifier to indicate global capture

var str = '123aaa456';
var reg = /\d+/; // Catch as many as you can once
var res = str.match(reg)
console.log(res) // ["123", index: 0, input: "123aaa456"]
reg = /\d+? /g; // Tackle greed and laziness
res = str.match(reg) console.log(res) // ["1", "2", "3", "4", "5", "6"]

Copy the code

The last

A regular expression mind map is attached