Several usage summaries of question marks in js regular expressions, and js Regular Expressions

Source: Internet
Author: User

Several usage summaries of question marks in js regular expressions, and js Regular Expressions

Add a question mark after a character that represents a repetition, for example, + ?, *?, {2, 3 }? You can stop matching greedy modes.

Var pattern =/\ w {2, 3}/; console. log ("aaaa ". match (pattern) [0]);/* result "aaa"; as many matches as possible in greedy mode, so it will match 3 repeated characters */var pattern2 =/\ w {2, 3 }? /; Console. log ("aaaa ". match (pattern2) [0]);/** result "aa"; after a question mark is added, the number of matching repetitions is as small as possible. * therefore, two duplicate characters are matched */

Used in a group? : Can generate unnumbered groups, such

Var pattern =/(AB) \ w + (ba)/; console. log ("abcba _". replace (pattern, "$1");/* result "AB _"; the matched characters are replaced by the first group (AB) */var pattern2 = /(? : AB) \ w + (ba)/; console. log ("abcba _". replace (pattern2, "$1");/** result "ba _"; added to the first group? :, Generates a * unnumbered group. Therefore, $1 matches the second group, and * is the text content matched by the first number group (ba */

(? =) And (?!); Zero-width positive-direction assertions and negative assertions. brackets indicate that the right side of a position must match the right side of =, or it cannot match! .

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". The right side of the r must be followed by * upper ings; And/string (? = S)/same; match "string"; the position behind g * must be on the right side of s. Although "string_x" also contains "string", * Does not meet (? =...) Conditions in parentheses */
Var pattern =/string (?! S)/; console. log ("strings ". match (pattern); // nullconsole. log ("string. ". match (pattern); // ["string"]/* (?!...) There cannot be one on the right of a location! * String (?! S)/matching "string", "g" cannot be followed by "s "*/

When it indicates the number of repeated times, it indicates that the number of repeated times is 0 or 1


Match the question mark of a regular expression

(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is useful when you use the "or" character (|) to combine each part of a pattern. For example, 'industr (? : Y | ies) is a simpler expression than 'industry | industries.
(?! Pattern) negative pre-query: matches the search string at the beginning of any string that does not match pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (?! 95 | 98 | NT | 2000) 'can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000 ". Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
Reference: www.yesky.com/185/1932685.shtml

How do regular expressions match question marks?

\? Here, \ is a quote, for example ..

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.