Js regular expression exec and match distinction

  • 2020-03-30 01:29:42
  • OfStack

In the past, js rarely used js regular expressions, even used to, is such as the name of the mail, such as the judgment, a lot of online code, very little research, to use.

Recently, I encountered some need to use regular expressions in my development

Regular expression objects are defined in two ways:

1. The first definition:

New RegExp (the pattern, the attributes); Var reg = new RegExp(" ABC ","g")

Where, pattern represents the content of the expression, as shown above, matching ABC

Attributes: g, global matching, I case-insensitive, m performs multi-line matching, the most used being g and I

2. The second definition :/pattern/attributes.

Var reg = / ABC /g;

Regular expression rules some rules are not stated here, only the difference between exec and match is recorded:

1. Exec is a regular expression method, not a string method. Its arguments are strings, as shown below:

The above definition
Var reg = new RegExp(" ABC ");
Var STR = "3abc4, 5abc6";
Reg. The exec (STR);  

2. A match is a string that executes a method to match a regular expression rule

Var reg = new RegExp(" ABC ");
Var STR = "3 abc4, 5 abc6";
STR. Match (reg);

3. Exec and match return arrays;

If the exec executes a regular expression that has no subexpression (the contents in parentheses, such as/ABC (\s*)/ (\s*)), returns the first matched string content if there is a match, the array has only one element, and returns null if there is no match.

Var reg = new RegExp(" ABC ");
Var STR = "3 abc4, 5 abc6";
Alert (reg. The exec (STR));
Alert (STR) match (reg));

If you execute the above code, you will find that both contents are the same: ABC,

4. If the regular expression object is defined as a global match, for example:

Var reg = new RegExp(" ABC ","g");
Var STR = "3 abc4, 5 abc6";
Alert (reg. The exec (STR));
Alert (STR) match (reg));

Is ABC and ABC, ABC; Because match performs a global match query; The exec, if it doesn't have a subexpression will only find a match and return.

5. When the expression contains a subexpression:

Var reg = new RegExp("a(BC)");
Var STR = "3 abc4, 5 abc6";
Alert (reg. The exec (STR));
Alert (STR) match (reg));

You will find that the results of both executions are: ABC, BC;

6. When the regular expression object is defined as a global match

Var reg = new RegExp("a(BC)","g");
Var STR = "3 abc4, 5 abc6";
Alert (reg. The exec (STR));
Alert (STR) match (reg));

Then they return ABC, BC and ABC, ABC,

Summarized as:

1. When the regular expression has no subexpression and is defined as a non-global match, the result of exec and match execution is the same, and both return the string content of the first match;

2. When the regular expression has no subexpression and is defined as a global match, exec and match execute. If there are multiple matches, then match returns an array of multiple elements.

3. When the regular expression has a sub-representation and is defined as a non-global match, the result of exec and match execution is the same as in case 5 above;

4. When the regular expression has a subexpression and is defined as a global match, the result of exec and match execution is not the same, then match will ignore the subexpression, only find the fully matched regular expression and return all contents, as in the sixth case above;

In other words, exec has nothing to do with whether the global is defined or not, while match is globally related, and when defined as non-global, both execute the same


Related articles: