ES2015 Regular Expression new feature, es2015 Regular Expression
New Features of ES2015 Regular Expressions:
Based on the regular expression, ES2015 enhances the support for four-byte unicode characters.
For more information about regular expressions, see the regular expression tutorial section.
1. Use of RegExp constructor:
Before ES2015, there are two ways to create a regular expression object using RegExp constructor:
Method 1:
Var reg = new RegExp ("antzone", "g ");
The first parameter of the constructor is the regular expression string body, and the second parameter is the regular expression modifier.
The above code is equivalent to the following code:
Var regex =/antzone/g;
Method 2:
Var reg = new RegExp (/antzone/g );
If the parameter is not a regular expression string, only one parameter is allowed. The following statement is incorrect:
Var reg = new RegExp (/antzone/, g );
You cannot use the second parameter to set a regular expression modifier.
ES2015 changes this line to. Even if the first parameter is a regular expression object, you can specify the second parameter:
Var reg = new RegExp (/antzone/gi, "g ");
The regular expression modifier specified by the second parameter will overwrite the modifier in the first parameter.
Ii. Regular Expression of strings:
The match (), replace (), search (), and split () methods related to regular expressions belong to string objects.
ES2015 modified this. When calling these four methods, the RegExp object instance method is actually called internally.
(1). String. prototype. match call RegExp. prototype [Symbol. match].
(2). String. prototype. replace call RegExp. prototype [Symbol. replace]
(3). String. prototype. search call RegExp. prototype [Symbol. search]
(3). String. prototype. split call RegExp. prototype [Symbol. split]
For more information about Symbol, see section ES2015 Symbol.
Iii. assertion (ES2016 ):
For more information about assertion, see the section "assertion with zero width" in the regular expression.
4. New modifier:
Modifier |
Description |
U Modifier |
This modifier identifies Unicode characters greater than \ uFFFF. |
Y Modifier |
It is specified that the matching can only start from the specified position in the lastIndex attribute. If the matching fails, no subsequent characters will be attempted. |
5. New attributes:
Attribute |
Description |
Sticky attributes |
Returns a Boolean value to identify whether the y modifier is set. |
Flags attributes |
Returns the modifier of the regular expression. |
6. New Methods:
Method |
Description |
RegExp. escape () (ES2016) |