Positive pre-checking and positive negation pre-checking in JS regular expressions

Source: Internet
Author: User

For those who have not used these expressions, there should be a little bit of understanding of the concept, and the following examples illustrate the users of these expressions in real-world cases.

A.:p attern--matching test:
Will act as a match check, is a non-fetch match, and appears in the match character result, such as Windows (?: 2000| nt|98) is equivalent to Windows2000|windowsnt|windows98
is a comparison with | A more abbreviated expression, with the direct use of | The difference is not returned as a sub-match:

Example 1:

var reg1=/windows (?: 2000| nt|98)/ivar reg2=/windows (2000| nt|98)/ivar str= ' windows2000 '//  ["Windows2000", index:0, Input: " Windows2000 "]//  [" Windows2000 "," $ ", index:0, Input:" Windows2000 "]reg1.test (str)    // truereg2.test (str)    //true

You can notice that the first regular match returns no sub-matches in the returned results

Two? =pattern--positive pre-check:

will be a match check, is a non-fetch match and will not appear in the matching result string.
Example:

var reg=/windows (? =2000| nt|98)/ivar str= ' Windows2000 'var str2= ' Windows XP '//  ["Windows", index:0, Input: "Windows2000"]str2.match (reg)    //null

which
1. Match windows, if there is no match, then return to empty
2. Are there any subsequent 2000| Nt|98 one of them, if there is, then return to Windows, no return to empty

Three?! pattern--positive negative pre-check:
Matches the find string at the beginning of any mismatched pattern string, and is a non-fetch match that does not appear in the matching result string.
Example:

var reg=/windows (?! 2000| nt|98)/ivar str= ' Windows2000 'var str2= ' Windows XP '//  null Str2.match (REG)    //["Windows", index:0, Input: "Windows XP"]

As you can see, it's just the opposite of the exact pre-check.

The above example is?! The front directly matches the string, and one more case, the meta-character, as in the following example:

var reg=/windows* (?! 2000| nt|98)/i    var str= ' Windows2000 'var str2= ' Windows XP '//  [" Window ", index:0, Input:" Windows2000 "]str2.match (reg)    //[" Windows ", index:0, Input:" Windows XP "]

* In the regular expression meaning is to match the previous sub-expression 0 or more times, then for STR, can match?! The expression in the back, so the inverse, that is, does not match the expression in the front (here is s), the match result is exactly the opposite of WINDOW,STR2.
Note: The following example may not be very good to understand, write a few expressions familiar with slowly understand.

Positive pre-checking and positive negation pre-checking in JS regular expressions

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.