In the past, JS rarely used JS regular expression. Even if it was used, it was judged such as mail names. There were many online code and few studies.
Recently, I have encountered some regular expressions that need to be used, and study by the way
There are two definition methods of regular expression objects ::
1. The first definition:
New RegExp (Pattern, Attributes); If var reg = new regexp ("abc", "g")
Among them, Pattern is the expression content of the expression, as if the above shows ABC
Attributes: G, global matching, I do not distinguish between small and lower case, M executes multi -line matching, the most uses of G and I
2. The second definition:/Pattern/Attributes.
Such as: var reg = /abc /g;
Some rules expressed in regular expression are no longer explained here, only the difference between EXEC and MATCH:
1. EXEC is a method of regular expression, not a string method. Its parameters are the string, as shown below:
As the above definition
var reg = new regexp ("ABC");
var str = "3ABC4, 5ABC6";
reg.exec (STR);
2. MATCH is a method for the string to perform regular expression rules. His parameters are regular expression, such as
var reg = new regexp ("ABC");
var Str = "3ABC4, 5ABC6";
str.match (eg);
3. EXEC and MATCH return are arrays;
If the regular expression executed by EXEC has no sub -expression (content in the small brackets, such as/s*)/(/s*)/(/s*), if there is a match, return the first matching string content content , There are only one element at this time, if not matched and returned to NULL;
var reg = new regexp ("ABC");
var Str = "3ABC4, 5ABC6";
alert (reg.exec (STR));
alert (str.match (reg));
Execute the same code, you will find that the content of the two is the same: ABC,
4. If the regular expression object is defined as global matching, as:
var reg = new regexp ("abc", "g");
var Str = "3ABC4, 5ABC6";
alert (reg.exec (STR));
alert (str.match (reg));
For ABC and ABC, ABC; because Match performs global matching query; EXEC will only find a matching one if there is no sub -expression.
5. When it means that the sub -expression is contained in:
var reg = new regexp ("a (bc)");
var Str = "3ABC4, 5ABC6";
alert (reg.exec (STR));
alert (str.match (reg));
You will find that the results of the two execute are: ABC, BC;
6. When the regular expression object is defined as a global match
var reg = new regexp ("a (bc)", "g");
var Str = "3ABC4, 5ABC6";
alert (reg.exec (STR));
alert (str.match (reg));
The result of the two returned is ABC, BC and ABC, ABC,
Summary as:
1. When the regular expression has no sub -expression, and is defined as non -global matching, the results of Exec and Match are the same, and the first matching string content is returned;
2. When the regular expression has no sub -expression, and is defined as global matching, Exec and Match execute, and there are multiple matching contents, then the Match returns multiple elemental arrays;
3. When the regular expression is represented, and is defined as a non -full -scale matching, the results of the execution of EXEC and Match are the same as above;
4. When the regular expression is represented, and defined as a global match, the results of Exec and Match are different. At this time, the Match will ignore the sub -expression, only find the full matching regular expression and return to all content, as above 6th, 6th, 6th, 6th, 6th, 6th, 6th. Planting;
That is to say, whether EXEC is defined by the overall situation is not related, and the MATCH is associated with the overall situation.