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