The small nine in regular


Regular also used a lot, and the regular to write their own truly is a few ah, not paste, paste is copy changes, sometimes for some regular also is not very understand the process of matching, think of this as a programmer basic knowledge should write their own regular is better, so look at the regular again, make a record, In order to prepare their own review and use;

The re is primarily used to manipulate strings

(1) Function

  1. Verify that the string is valid
  2. Find the string that matches the specified characteristics
  3. Substitution string

(2) Prerequisite knowledge points

  1. Zero-width: a subexpression is considered zero-width if it matches only one position, or if the matching content is not saved in the final matching result (in fact, the start and end positions of the expression match are the same position); For example: “^ $(? =xxx) (? ! =xxx)”
  2. Possession character: subexpression matches character content, not position, and is saved in the final match result
var reg = /abc/; You can think of /a/ /b/ /c/ as subexpressionsCopy the code

(3) Expressions

  1. Declaration method:

    1. Constructor:
          var reg1 = new RegExp("xyz".'gi'); // Parameter 1: regular content // parameter 2: modifiers; // I: case insensitive; // g: global matching; // m: multi-line match, encounter newline does not end, match to the end of the string;Copy the code
    2. Literal:
          var reg = /xyz/gi;
      Copy the code
  2. Methods:

    1. Reg. The test () / / return true | false
          var reg = /zyx/i;
          var str="zyxaa"
          reg.test(str) // true;
      Copy the code
    2. Exec and compile methods are self-explanatory
    3. The rest use string methods, not regular methods;
  3. Match multiple characters

expression Match the range placeholder expression Match the range
\d Any number, from 0 to 9 \D matches non-\ W \w Any letter or number or underscore
\s Any of the whitespace characters, including Spaces, tabs, and page feeds \B match position (\ B take not position) \b Matches a word boundary, a position :(one side is the “\w” range, the other side is the non-” \w” range)
. Any character other than newline (\n) 12 [^ a-z] take complement [12A-Z] Matches any 1 or 2 or A through Z characters in brackets
^ Matches the starting position, not any characters $ Matching end position
| The relationship between these two expressions is or (a) Start and end positions of a subexpression
1. Note that many metacharacters become normal characters within character groups, such as (^$?). Such as [$^?] That's the matching point and the $^, right? [^] ^ is the complement in the first positionCopy the code
  1. The number of modified
expression Match the range placeholder expression Match the range
{n} Repeated n times {m,} Repeat at least m times
{m,n} The number of repetitions is in the range [m,n] ? Repeating 0 to 1 is equivalent to {0,1}
+ Number of repetitions greater than or equal to 1 equals {1,} * Repeat any number of times equal to {0,}

(4) Matching process

  1. For the entire expression, the attempt starts at position 0 in the string, with backtracking and so on. If the match is successful, it returns, and then the match continues from position 1. If the match fails, the previous match starts again, and so on, until it succeeds or fails.
  2. For subexpression: the position where the match starts is the position where the previous expression successfully ends.
  3. For example, the following matches:

(5) Some special usage

  1. Match the number of greedy and non-greedy

    1. Greedy mode: The default is greedy mode: matches as many strings as possible without making the entire expression match

    1. Non-greedy matching: in the number of matches modifier ({m,n}/? +*) followed by “?” Match as few strings as possible on the premise of making the whole expression match successfully;

  2. Capture group reference:

    1. The parentheses “()” are used for grouping in re:
      1. When modifying the number of matches, the expression in parentheses can be modified as a whole.
      2. When matching the result, the content matched by the expression in parentheses can be obtained separately.
    2. Capture group: It is to save the content matched by the regular expression subexpression (expression in parentheses) to the numbered or explicitly named group in memory, which is easy to reference later.
    3. Reference: You can reference \number (reverse reference) inside the regular expression or $number outside the regular expression
      1. The reference to number corresponds to the order in which the parentheses appear to the left of the subexpression
      2. Subexpression inside parentheses “()”, if you want the match result not to be recorded for later use, you can use “(? Format: XXXXX)”

  3. Regular assertions (forward, negative, antecedent, backward)

    The following assertion is not supported in js.

    1. (? // represents a position in the string immediately following which the sequence of characters matches pattern.
    2. (? ! // represents a position in the string immediately following which character sequences do not match pattern.

(6) Common regular expressions

Finally, here are some regular expressions for you to useCopy the code
  1. Only alphanumeric underscores are allowed. They must contain case, numbers and underscores
var reg=/^(? =. * ([0-9])? =.*[a-z])(? =.*[A-Z])(? =. * \ \ w _) 8, 20} {$/;Copy the code
  1. Four digits and one blank space of bank card:
    var str="888888888888888888";
   var newStr= str.replace(/\s/g, ' ').replace(/(.{4})/g, "The $1 ");
Copy the code
  1. Interesting re:
var reg = /[^\w.$]/; Reg matches non-alphanumeric underscore dots and $(matches special characters and Chinese characters)Copy the code