Deep understanding of regular expression syntax and deep understanding of Regular Expressions

Source: Internet
Author: User

Deep understanding of regular expression syntax and deep understanding of Regular Expressions

4. Selection and grouping

(1). Group

Character group []: Indicates matching one of several characters

Character groups can be understood as combinations of characters. The difference between character groups and common characters is: the abc common character indicates matching a and B, followed by c, and the character group [abc] indicates matching a, B, or c at the same position; the meaning of the character Group determines that the character group can be considered as a common special character.

Common: It is common because it also indicates that the operation of matching a character at a position on a single character can also work on it; for example: [a-z] {6 };

Special: the special reason is: 1. other characters may have different meanings inside and outside, such :-,., ^, etc., 2. you can select more than one character at the same position;

Differences between characters inside and outside the character group:

-: Within the character group, it indicates a range. Outside the character group, it indicates only one character;

Example:

-Out of the character group

<script type="text/javascript">var str = '2016-8-20';var reg = /16-8/g;console.log(str.match(reg)); //16-8</script>

-In the character group

<script type="text/javascript">var str = '2016-8-20';reg = /1[6-8]/g;console.log(str.match(reg)); //16 </script>

-A common character outside the character group only represents-this character itself, so it matches 1, followed by 6, followed by-followed by 8, so it is 16-8.

-In the character group, a Range is displayed. Therefore, the matching result is a string of any number between 6 and 8. Therefore, the character is 16;

.: Represents a point within the character group, and any character outside the character group (find a single character, except for line breaks and line terminator ).

. Outside the character group

<script type="text/javascript">var str = '2016-8-20';var reg = /6.8/g;console.log(str.match(reg)); //6-8</script>

. In the character group

<script type="text/javascript">var str = '2016-8-20';var reg = /6[.]8/g;console.log(str.match(reg)); //null</script>

. It indicates any character outside the character group. Therefore, it matches 6 followed by any character followed by a string of 8, so it matches 6-8;

. In the character group, only one common character indicates itself. Therefore, it matches 6. Next, it is a string of 8. It is null because it does not match;

Character groups can also be divided into matching character groups and excluded character groups mainly because of changes in the meaning of ^

^: Delimiters;

Example:

^ Outside the character group

<script type="text/javascript">var str = 'han';var reg = /^ha/g;console.log(str.match(reg)); //ha</script>

^ In the character group

<script type="text/javascript">var str = 'han';var reg = /[^0-9]/g;console.log(str.match(reg)); // h,a,n</script>

The escape character is an anchor outside of [], indicating that it matches a character or string starting with a certain character, except

Note: [^ a] indicates matching a character whose position is not equal to a. do not consider it as a. The latter can match a blank line. Note the difference.

Example:

<script type="text/javascript">var str = 'han';var reg = /n[^q]/g;console.log(str.match(reg)); //null</script>

Note: In this example, there are no characters after n, so the matching result is null. According to the second understanding above, the matching result may be considered as: n

[] No matter how many characters there are, only one of them will be matched at the same position. What if I want to match a string at a position?

(2). Select

In this case, you can use () plus | this combination, | which means or in the regular expression, while () divides a range in the regular expression. Separate () it makes no sense to use it. Only when it is used together with other quantifiers can it reflect its role (let's talk about the simple example, greedy matching and non-Greedy matching, and then let us talk about it separately, I will not describe it here );

Example:

<script type="text/javascript">var str = '2016-8-20';var reg = /(16-8)/g;console.log(str.match(reg)); //16-8</script><script type="text/javascript">var str = '2016-8-20';var reg = /16-8/g;console.log(str.match(reg)); //16-8</script>

In this example, if all the brackets are the same, they indicate matching the string 16-8.

When () and | are used together, it is a multiple-choice structure. Matching any subexpression () represents a range. | it represents a selection direction.

Example:

<script type="text/javascript">var str = 'greygray';var reg = /gr[ea]y/g;console.log(str.match(reg)); // grey,grayreg = /gr(e|a)y/g;console.log(str.match(reg)); //grey,gray</script>

The above two matching results are gray. gray means that the Branch selection Structure of (|) can be replaced with [] to a certain extent.

Example:

<script type="text/javascript">var str = 'greygray';var reg = /gre|ay|yg/g;console.log(str.match(reg)); //gre,yg,ay</script>

Output result: gre, yg, ay

The addition of parentheses has a major impact on the results, for example:

^ Hello | world | hi: matches ^ hello, world, or hi.

^ (Hello | world | hi): the starting line for matching is ^ hello, ^ world, or ^ hi.

Note: Although multiple-choice structures can be represented by character groups, they can represent each other, but a character group can only match a single character in the target text, each multiple-choice structure may be a complete regular expression and can match texts of any length.

Notes in Regular Expressions:

1. In regular expressions, space characters are also considered as a rule character;

2.-;.; *. The meanings of metacharacters are different between [] and;

3. After adding [] characters to a string, the string is considered as a character group, and the string operated on a single character? * You can also use them. The mixed use of quantifiers and the mixed use of quantifiers and group selection have a greedy and non-Greedy match concept, which will be separately summarized;

4. Differences between the branch structure and character group; the branch can match texts of any length; the character group can only match one character;

5. note some concepts: for example, [^ x] indicates 'matching a character not equal to x' rather than 'matching only when this position is not 'X '; [^ x] the concept behind it can match an empty row, for example, {x, y },?, * Understanding;

6. In the matching process, x and x in {x, y} determine whether the matching is successful.

The above is a complete description of the regular expression syntax. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. on this page, I would like to thank you for your support for the help House website!

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.