JavaScript regular Expressions (? =exp), (? <=exp), (?! Exp

Source: Internet
Author: User

(? =exp)

Baidu Encyclopedia to explain: non-get matching, positive pre-check, at any match pattern at the beginning of the string to match the search string, the match does not need to obtain for later use. For example, "Windows (? =95|98| nt|2000) "Can match" windows "in" Windows2000 ", but does not match" windows "in" Windows3.1 ". Pre-checking does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, rather than starting with the character that contains the pre-check.

Look at this, I'm afraid I can not understand the whole life.

Let's look at a case:

// results: ["123", "456"]

Explain:

First to satisfy: \d{1,3} this regular.

1th Step: The start of 1 satisfies \d{1,3} maximum match, the latter 23456789.56 does not meet (? = (\d{3}) + (\.\d*)? $), so this "1" is not satisfied.

2nd step: The start of 12 satisfies \d{1,3} maximum match, the latter 3456789.56 does not meet (? = (\d{3}) + (\.\d*)? $), so this "12" is not satisfied.

3rd Step: The beginning of the 123 satisfies \d{1,3} maximum match, the following 456789.56 satisfies (? = (\d{3}) + (\.\d*)? $), so this "123" satisfies, returns "123"

4th step: Starting from 4 to search, the beginning of the 4 meet \d{1,3} maximum match, the following 56789.56 is not satisfied (? = (\d{3}) + (\.\d*)? $), so this "4" is not satisfied.

5th step: Starting from 5 to search, the beginning of the 45 meet \d{1,3} maximum match, the following 6789.56 is not satisfied (? = (\d{3}) + (\.\d*)? $), so this "45" is not satisfied.

6th step: Starting from 6 to search, the start of 456 satisfies \d{1,3} maximum match, the following 789.56 satisfies (? = (\d{3}) + (\.\d*)? $), so this "456" satisfies, returns "456"

7th step: Starting from 7 to search, the beginning of the 7 meet \d{1,3} maximum match, the following 89.56 is not satisfied (? = (\d{3}) + (\.\d*)? $), so this "7" is not satisfied.

8th step: Starting from 8 to search, the beginning of the 78 meet \d{1,3} maximum match, the following 9.56 is not satisfied (? = (\d{3}) + (\.\d*)? $), so this "78" is not satisfied.

9th step: Starting from 9 to search, the beginning of the 789 meet \d{1,3} maximum match, the following. 56 is not satisfied (? = (\d{3}) + (\.\d*), $), so this "789" is not satisfied, at this time "789" satisfies the maximum match of \d{1,3}, but there is no match at the back.

10th step: From. Start a search, start a. (. | 9. | 89.) are not satisfied \d{1,3}, so this from. Not satisfied with the location search.

11th Step: Starting from 5 to search, the beginning of the 5 meet \d{1,3} maximum match, the following 6 is not satisfied (? = (\d{3}) + (\.\d*)? $), so this "5" is not satisfied.

12th step: Starting from 6 to search, the beginning of the 56 meet \d{1,3} maximum match, there is no, not satisfied (? = (\d{3}) + (\.\d*)? $), so this "56" is not satisfied.

The direct point is that: (? =EXP) The matching content is not captured by match, but only when a condition is used to determine whether the condition is satisfied after the current location of the search.

(? <=exp)

Also called 0 width is recalling the post assertion, which asserts that the front of the position itself appears to match the expression exp. For example (<=\bre) \w+\b matches the second half of a word that begins with re (except for parts other than re), such as when looking for reading a book, which matches ading.

A bit strange, in the JS regular expression to test this has been an error, guess estimation is not supported.

(?! Exp

This is the opposite of (? =exp).

Console.log (' 123456789.56 '). Match (/\d{1,3} (?! (\d{3}) + (\.\d*)? $)/g)); // ["345 ", "678", "9", "the"]

Explain:

First of all Meet \d{1,3}

Starting at 1, the maximum match for \d{1,3} is 1, followed by 23456789.56, meet, (?! (d{3}) + (\.\d*)? $), OK, at this point \d{1,3} matches "1"

The second time starting from 2 search,, \d{1,3} The maximum match is 12, followed by 3456789.56, meet, (?! (d{3}) + (\.\d*)? $), OK, at this point \d{1,3} matches "12"

The third time starting from 3 search, \d{1,3} The maximum match is 123, followed by 456789.56, not satisfied, (?! (d{3}) + (\.\d*), so the first of the returned results array is "12", at which point the maximum of \d{1,3} matches is "12"

The fourth time starting from 4 search, followed by 56789.56, \d{1,3} The maximum match is 34, meet, (?! (d{3}) + (\.\d*)? $), OK, at this point \d{1,3} matches "34"

The fifth time starting from 5 search, followed by 6789.56, \d{1,3} The maximum match is 345, meet, (?! (d{3}) + (\.\d*)? $), OK, at this point \d{1,3} matches "345"., the maximum 3 digits have been met, this time return "345"

The sixth time starting from 6 search, followed by 789.56, \d{1,3} The maximum match is 6, not satisfied, (?! (d{3}) + (\.\d*), $), at which point \d{1,3} matches "6".

The seventh time starting from 7 search, followed by 89.56, \d{1,3} The maximum match is 67, meet, (?! (d{3}) + (\.\d*), $), at which point \d{1,3} matches "67".

The eighth time starting from 8 search, followed by 9.56, \d{1,3} The maximum match is 678, meet, (?! (d{3}) + (\.\d*)? $), at which point \d{1,3} matches "678", and the maximum of 3 digits has been met, at which point "678" is returned.

The nineth time starting from 9, followed by. \d{1,3} The maximum match is 9, meet, (?! (d{3}) + (\.\d*), $), at which point \d{1,3} matches "9"

Tenth time from. Start the search, at this time "9." does not satisfy \d{1,3}, so at this time return "9"

The 11th time starting from 5 search, followed by 6, \d{1,3} The maximum match is 6, meet, (?! (d{3}) + (\.\d*), $), at which point the maximum match for \d{1,3} is "5"

The 12th time starting from 6 search, after No, \d{1,3} The maximum match is 56, meet, (?! (d{3}) + (\.\d*) ($), at this point \d{1,3} maximum match is "56", end, return the last "56"

This is only a personal understanding, there is something wrong, hope that the great God to correct me.

JavaScript regular Expressions (? =exp), (? <=exp), (?! Exp

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.