JS regular expressions from getting started to being buried (7)--Grouping

Source: Internet
Author: User

Group

When using the regular, sometimes want to match a string of strings successive occurrences, such as: I want to match the string Byron 3 consecutive occurrences of the case.

Some people will write directly:

Byron{3}

However, this situation will only match Byro plus three n , obviously, this is wrong.

To achieve the previous requirements, we need to use the grouping function of regular expressions: using the () functions that can achieve grouping, the quantifier acts on the grouping. Therefore, the following can be successfully matched Byron three times

(Byron){3}
Or

The meaning of JS | is or, in the regular, we can also use | the effect achieved or. The more commonly used or written notation is:

Byron | Casper

That is, Byron or Casper two select one.

There is also a usage that is used in conjunction with grouping:

let text = ‘ByronsperByrCasper‘let reg = /Byr(on|Ca)sper/g     // 匹配Byronsper或ByrCaspertext.replace(reg, ‘X‘)      // XX
Reverse reference

At this point, we have a requirement to convert a format time string: To a yyyy-MM-DD MM/DD/yyyy type format string.

The difficulty of this problem is not only to change the position of the - / month and day. Since the numerical value is not fixed, it cannot be converted directly to a fixed value. At this point, we can use a reverse reference to solve the problem.

A reverse reference is an important high-level syntax in a grouping that is primarily used to solve the problem of grouping variables.

What is a grouping variable? A grouping variable is a value that is matched by a grouping, and the regular expression turns it into a variable that can be called. The call to this variable is also very simple $n n represents the sequence number of the grouping, it is worth mentioning that the sequence number is starting from 1, rather than starting from 0.

The reverse reference makes it easy to solve the problem above:

let text = ‘2018-07-09‘let reg = /(\d{4})-(\d{2})-(\d{2})/g/*    $1是(\d{4})的匹配内容,代表yyyy    $2是第一个(\d{2})的匹配内容,代表MM    $3是第二个(\d{2})的匹配匹配内容,代表DD*/text.replace(reg, ‘$2/$3/$1‘)   // 07/09/8
Ignore grouping

When you do not want to capture certain groupings, you only need to add them within the grouping ?:

(?:Byron).(ok)

JS regular expressions from getting started to being buried (7)--grouping

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.