In the past, I rarely used regular expressions of js. Even if I used it, I also used them to judge such as email names. There is a lot of online code and there are very few researches, so I can use it as soon as possible.
Recently, I encountered some development needs to use regular expressions, so I will study it by the way
There are two ways to define regular expression objects:
1. The first definition:
new RegExp(pattern, attributes); such as var reg = new RegExp("abc","g")
where pattern represents the content of the expression, as shown above, match abc
attributes: g, global matching, i is case-insensitive, m performs multi-line matching, the most used is g and i
2. The second definition: /pattern/attributes.
For example: var reg = /abc/g;
Rules of regular expressions Some rules will not be explained here, only the difference between exec and match:
1. Exec is a method of regular expressions, not a method of strings. Its parameters are strings, as shown below:
As defined above
var reg = new RegExp("abc") ;
var str = "3abc4,5abc6";
(str );
2. Match is a method for strings to execute matching regular expression rules. Its parameters are regular expressions, such as
var reg = new RegExp("abc") ;
var str = "3abc4,5abc6";
(reg);
3. The exec and match return are arrays;
If the regular expression executed by exec does not have a subexpression (the content in brackets, such as (\s*) in /abc(\s*)/), if there is a match, the first matching string content will be returned. At this time, the array has only one element, and if there is no match, it will return null;
var reg = new RegExp("abc") ;
var str = "3abc4,5abc6";
alert((str));
alert((reg));
Execute the above code and you will find that both contents are the same: abc,
4. If the regular expression object is defined as a global match, such as:
var reg = new RegExp("abc","g") ;
var str = "3abc4,5abc6";
alert((str));
alert((reg));
Then it is abc and abc, abc; because match executes a global match query; and if exec does not have a subexpression, it will only find a match, that is, it will return.
5. When the representation contains subexpressions:
var reg = new RegExp("a(bc)") ;
var str = "3abc4,5abc6";
alert((str));
alert((reg));
You will find that the results of both execution are: abc, bc;
6. If the regular expression object is defined as a global match
var reg = new RegExp("a(bc)","g") ;
var str = "3abc4,5abc6";
alert((str));
alert((reg));
Then the results returned by both are abc, bc and abc, abc,
Summary as:
1. When the regular expression has no child expression and is defined as a non-global match, the results of exec and match execution are the same, both return the first matching string content;
2. When the regular expression has no child expression and is defined as a global match, exec and match are executed to make multiple matching contents exist, 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 results of exec and match execution are the same as in the fifth case above;
4. When the regular expression has a sub-representation and is defined as a global match, the results of exec and match execution are different. At this time, the match will ignore the sub-expression, only find the fully matched regular expression and return all contents, as in the above 6th case;
In other words, exec has nothing to do with whether the global is defined, while match is related to the global. When defined as non-global, the execution results of both are the same.