I am small and live in Wuhan, do two years of new media, ready to use 6 months time to switch to the front end of the industry.

Lesson Objective for today

Yesterday we learned explicit archetypes, implicit archetypes, new and Instanceof based on search. Today is mainly based on the search to learn RegExp statement basis, I have a basic look at the content, feel that it will be divided into several days to learn RegExp ~~~~, another day for learning, come on, small and !!!!


how

Literals, constructors, and factory symbols are all ok:

/ / literal
/pattern/flags

// constructor
new RegExp(pattern [, flags])
 // Factory symbol RegExp(pattern [, flags]) Copy the code

Parameters that

pattern

Text of the regular expression.


flags

If specified, flags can have any combination of the following values:

The name of the specify
g Global matching; Find all matches instead of stopping after the first match
i Ignore case
m Multiple lines; Will start and end characters (^and$) is considered to work on multiple lines (that is, match the start and end of each line respectively (by\n\rSplit), instead of just matching the beginning and end of the entire input string.
u Unicode; View patterns asUnicodeA sequence of sequence points
y Viscous matching; Matches only this in the target stringRegular expressionthelastIndexAttribute indicates the index (and does not attempt to retrieve from anysubsequenttheThe indexMatch).

use

Based on using

There are two ways to create a RegExp object: a literal and a constructor.

If pattern is a string or literal, the arguments do not need to be quoted, and the constructor arguments use quotation marks.

Therefore, the following expression creates the same regular expression:

/ / literal
/ab+c/i;

// constructor
new RegExp('ab+c'.'i');
 // Factory symbol new RegExp(/ab+c/.'i'); Copy the code

recompile

The literal form provides compilation state of the regular expression when an expression is assigned, and the literal is used when the regular expression remains constant.

For example, when you construct a regular expression using literals in a loop, the regular expression is not recompiled on each iteration.

The constructors of regular expression objects, such as new RegExp(‘ab+c’), provide a runtime compilation of regular expressions.

Constructors can be used if you know the regular expression pattern is going to change, or if you don’t know what pattern in advance and get it from another source, such as user input.

This passage is a little tricky


Character escape rules

When using constructors to create regular objects, you need the normal character escape rules (preceded by a backslash \). For example, the following is equivalent:


new RegExp("\\w+");

/\w+/;
Copy the code

New support for ES6

In ES5, when the first pattern is a regular object, it is not allowed to use the second parameter to add modifiers. Otherwise, TypeError will be reported (Uncaught TypeError: Cannot supply flags when CONSTRUCTING one RegExp from another)

const regex = new RegExp(/xyz/.'i');
// Uncaught TypeError: Cannot supply flags when constructing one RegExp from another
Flags are not supported when constructing from other regular expressions
Copy the code

ES6 changes this behavior. If the first pattern of the RegExp constructor is a regular object, the second argument can be used to specify the modifier.

Furthermore, the returned regular expression ignores the modifier of the original regular expression and uses only the newly specified modifier.


new RegExp(/abc/ig.'i').flags
// "i"
Copy the code

In the code above, the modifier of the original re object is ig, which is overridden by the second argument I.



Summary of today’s lesson



Today the mood

Today is mainly based on the search to learn RegExp declaration basis, know how to declare RegExp, and basic use, hope to learn more content tomorrow RegExp ~~~~


This article is formatted using MDNICE