Several usages of the question mark in JS regular expression--regular expressions

Source: Internet
Author: User
Tags regular expression

Add a question mark after the character that represents the duplicate, such as +?,*?,{2,3}? You can stop matching greedy patterns.

var pattern=/\w{2,3}/;
Console.log ("AAAA". Match (pattern) [0]);
/* result "AAA"; greedy mode will match as many as possible,
so it will match 3 duplicate characters
* *
var pattern2=/\w{2,3}?/;
Console.log ("AAAA". Match (PATTERN2) [0]);
* * results "AA", add a question mark will be as little as possible to match the number of repetitions,
* so match to 2 duplicate characters * *

Used in groups?: You can produce groups without numbers, such as

var pattern=/(AB) \w+ (BA)/;
Console.log ("Abcba_". Replace (Pattern, "$");
/* result "ab_"; the character to be matched is replaced by the first grouping (AB)
* * *
/var pattern2=/(?: AB) \w+ (BA)/;
Console.log ("Abcba_". Replace (PATTERN2, "$"));
* * result "ba_"; in the first group?:, the result is a
* No number of groupings, so the matching character is the second grouping, * is the
First Number group (BA) match the text content
* *

(? =) and (?!); 0 wide forward assertion and negative assertion, which indicates that the right side of a position must match the = right or not the character after the.

var pattern=/str (? =ings) ing/;
Console.log ("Strings.a". Match (pattern));
Console.log ("Strings.a". Match (/string (? =s)/));
Console.log ("String_x". Match (pattern));
Console.log ("String_x". Match (/string (? =s)/));
/* The first two results are ["string"] and the last two results are null;
*str (? =ings) ing/matches "string", and the right side of R must follow the
ings on * and/string (? =s)/same; match "string"; g. The
right side of the * Must be followed by an S. "String_x" may also contain "string" but
* is not satisfied (? = ...) Conditions in parentheses
* *
var pattern=/string (?!) s)/;
Console.log ("Strings". Match (pattern));//null
Console.log ("string."). Match (pattern));//["string"]
/* (?! ...) A position on the right can not have! After the matching characters,
*string (?!) s)/Match "string", "G" can not be followed by "s"
* *

When the number of repetitions is indicated, the representative repeats 0 or 1 times

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.