ES2015 Regular Expression new feature, es2015 Regular Expression

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.