JavaScript Regular expression Quick judgment tips

Source: Internet
Author: User
Tags control characters

Text: JavaScript Regular expression Quick judgment skill

Here is a bit of JS's experience, not the most complete rule summary, more emphasis on the actual use of the rapid judgment, beginner contact is often followed by a bunch of star brackets to stun, with a number of quick-judging techniques can be grasped from the overall and not flustered.

  The core of JS regular quick judgment technique is: Pay attention to parentheses and question marks . This has been able to cope with most of the practical applications.

First, parentheses

  It is well known that the brackets in the regular denote ranges, curly braces denote repetitions, and parentheses denote grouping.

parentheses denote grouping, but there are many articles within the grouping, broken down:

(1): The parentheses themselves represent groupings, and the matching items are saved, which is called the trapping grouping, which is the default, such as:var c= "a001";  /A (\d*)/.test (c);    Console.log (regexp.$1); 001var num= "1234 5678"; var newnum=num.replace (/(\d{4}) (\d{4})/, "$ $");(2): If you do not want to save, the use of non-capturing groups (?:), such as:var c= "a001"; /A (?: \  d*)/.test (c);     Console.log (regexp.$1); Empty(3): Parentheses in the grouping, you can use a qualifier on the character combination, and then can follow the number of times, such as: (a\d) {2} match a3a5(4): Select one of the multiple direct quantities in the group. For example: g (a|a) Y, but this situation is faster with g[aa]y(5): When there is a nested parenthesis in the pattern, the number of the variable is done one at a time as the parentheses begin to appear, such as:/([A-za-z] (\d{2})) ((-) \d{2})/Match "a22-33", the following is the case:$1:a22, $2:22, $ -33, $4:-(6): (?:) Represents a non-capturing grouping, (? =) represents a forward forward grouping, and a (? =reg) matches a followed by a reg;(7): (?!) Indicates negative forward grouping, a (?! REG) matches A that is not followed by Reg;(8): (? <=) and (? <!) Indicates forward and negative backward,JavaScript does not support post-juniper ;The previously mentioned capturing grouping, which derives from a reverse reference, refers to the use of the content saved in the preceding capturing group in the regular expression, with "\" plus a sequence number to refer to, such as/(dog) \1/can match "Dogdog". second, question markThere are three ways to remember the use of question marks, except for the use of parentheses inside. Also remember the following two usages:1, if the question mark is directly followed by other characters, then as a simple quantifier, indicating the occurrence of 0 or one time, the simple quantifier also includes: *,+,{n},{n,m},{n,}. 2, when the question mark itself is immediately after the other simple quantifier, it is no longer as a simple quantifier, but changed the classifier attribute, from greedy quantifier to lazy quantifier. Greedy quantifier means that at the time of matching the whole string of the first breath, found that can not match, remove the last character, and then match, until the length of zero. The lazy quantifier is the opposite, matches one character at a time, and finds that the mismatch is matched again. (There is also a dominant quantifier, after a simple quantifier is connected to the plus sign, the first two kinds of quantifiers will be repeated attempts, and the control of quantifiers only try once, failed to forget. JavaScript does not support the domination of quantifiers. ). all simple quantifiers are themselves greedy quantifiers. For example:The first item of the array represents a string in the string that matches the entire pattern, and each entry, if any, holds a string that matches the capturing group in the regular expression. See, in fact, there is not much, as long as the use of the parentheses and the question mark is remembered, you can quickly split the regular and then understand, so that not a bunch of question marks star brackets to stun. Of course, some of the most basic knowledge is to have, but the nature of pure memory, relatively simple, with some common knowledge:
character Regular Description
\ t /\t/ Tabs
\ n /\n/ Tabs
\ r /\r/ Carriage return character
\f /\f/ Page break
\a /\a/ Alert character
\e /\e/ Escape character
\cx /\cx/ control characters corresponding to X
\b /\b/ With fallback character
\v /\v/ Vertical tab
/ /\0/ Null character
character equivalent to Description
. [^\n\r] Any character other than line break and carriage return
\d [0-9] numeric characters
\d [^0-9] Non-numeric characters
\s [\t\n\x0b\f\r] White space characters
\s [^ \t\n\x0b\f\r] Non-whitespace characters
\w [A-za-z_0-9] Word character (all letters)
\w [^a-za-z_0-9] Non-word characters
   

JavaScript Regular expression Quick judgment tips

Related Article

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.