Several usages of the question mark of JS regular expression

Source: Internet
Author: User

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

1 varpattern=/\w{2,3}/;2Console.log ("AAAA". Match (pattern) [0]);3 /*The result is "AAA"; the greedy mode will match as many as possible,4 so it matches 3 duplicate characters.5 */6 varpattern2=/\w{2,3}?/;7Console.log ("AAAA". Match (PATTERN2) [0]);8 /*9 * result "AA"; add question mark will be as few as possible to match the number of repetitions,Ten * So match to 2 duplicate characters One */

Use within a group?: You can generate non-numbered groupings, such as

1  varpattern=/(AB) \w+ (BA)/;2Console.log ("Abcba_". Replace (Pattern, "$"));3   /*The result "ab_"; the character matched to the first group (AB)4 * Replace5  */6   varpattern2=/(?: AB) \w+ (BA)/;7Console.log ("Abcba_". Replace (PATTERN2, "$"));8   /*9 * result "ba_"; added in the first group?:, produces aTen * There are no numbered groupings, so the matching characters are the second grouping, One * That is, the first number grouping (BA) matches the text content A  */

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

1 varPattern=/str (? =ings) ing/;2Console.log ("STRINGS.A". Match (pattern));3Console.log ("Strings.a". Match (/string (? =s)/));4Console.log ("String_x". Match (pattern));5Console.log ("String_x". Match (/string (? =s)/));6 /*The first two results are ["string"], and the latter two results are null;7 */STR (? =ings) ing/matches "string", and R to the right of the position must be followed by8* Upper Ings; and/string (? =s)/same as; Match "string"; The position behind the G9* The right side must be followed by an S. "String_x", although it also contains "string"butTen* Not satisfied (? =...) The conditions in parentheses One*/
1 var pattern=/string (?! s)/; 2 console.log ("strings". Match (pattern)); // NULL 3 console.log ("string.". Match (pattern)); // ["string"] 4 /* (?! ...) The right side of a position cannot have! After matching characters,5*/string (?! s)/Match "string", "G" cannot be followed by "s"6 */

Represents repeated 0 or 1 times when the number of repetitions is expressed

Several usages of the question mark of JS regular expression

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.