In the previous project development, I found that using the regular method can improve the development efficiency, so I looked at the tutorials and blogs and re-learned the rules and recorded them.

#### Regular statement

• Constructor declaration mode
var pat = new RegExp(pattern,modifiers);
• Literal declaration
var pat = /pattern/modifiers
• Tip: Parameter explanation

:
pattern: pattern, the content to be matched.
Modifiers: modifiers

• i:ignore. Case-insensitive matching
• g:global. Global match
• m: Multi-line matching
ps: These three can be used together, without order.
```var reg = new RegExp('abc','ig'); // no case and global mathabc。
var reg = /abc/m  // multil line math abc。
```

#### Regular character

• ^ indicates the start position of the matching input string
^abc => match first abc
• \$ means match the end of the input string
abc\$ => match the last abc
• Represents any character other than the newline character \n
. <=> \w\d\r\f\t\v
ps:[.] This pattern represents only matching . characters, equivalent to ‘.’
• [] represents a collection of characters. Match any one of the characters
[abc] => apple also match [abc]’s a
[abcdef] => football match [abcdef]’s a,b,f
[^abc] => match no abc
ps: ^abc and [^abc]:

• ^abc represents the match of the first string in the entire character to abc
• [^abc] represents any character that does not contain abc
• {} indicates the number of matching characters, quantifiers
a{2} => Representative match determines that a can only appear twice

• {n} matches the determined n times
a{2} => Representative match determines that a can only appear twice aa
• {n,} means at least n matches
a{2,} => The representative match determines that a appears at least twice aa, aaa, aaaa
• {m,n} means at least m times, up to n times
a{2,4} => The representative match determines that a appears at least 2 times, and at most 4 times aa, aaa, aaaa
• | indicates a relationship or both, which can match
a|b => Representing both a and b can match
• () indicates a grouping character, one for matching the characters inside and getting this match. The second represents the priority
(partten) => Match pattern and get this match
^(0|[1-9][0-9]*)\$ => Numbers that begin with zero and non-zero
ps:(?:pattern) => matches the pattern but does not get the matching result.
• + means that the preceding character must appear at least once (1 or more times)
a+bc <=> abc | aabc | aaabc
ps:+ is equivalent to {1,}
• * indicates that the character may not appear, or one or more times
a*bc <=> bc | abc | aabc
ps: * is equivalent to {0,}
• ? indicates that the preceding characters can only appear at most once (0 times or 1 time)
a?bc <=> bc | abc

#### Common characters and non-printing characters

• \d matches a numeric character
\d is equivalent to [0-9] ( \d <=> \[0-9\])
• \D matches a non-numeric character
\D is equivalent to [^abc] ( \D <=> \[^0-9\])
• \w matches letters, numbers, underscores
\w <=> \[A-Za-z0-9_\]
• \W matches non-letters, numbers, underscores
\W <=> \[^A-Za-z0-9_\]
• \b matches a word boundary, which is the position between the word and the space
• \B matches a non-word boundary
eg: /ter\b/ => matches the chapter and does not match the terminal
• \n newline
• \r carriage return
• \f page break
• \t tab
• \v vertical tab
• \s matches any whitespace characters, including spaces, tabs, page breaks
\s <=> \[\f\n\v\t\r\]
• \S matches any non-whitespace characters
\S <=> \[^\f\n\r\v\t\]

#### Regular method

var reg = new RegExp()
var str = ‘abc’; typeof str == ‘string’

• Reg.test(str); is used to test whether a string matches a regular, the return value is a boolean value, the match is true, and the match is false.
```  var reg = /^a?b+c*/
var str1 = 'aabbcc'
var str2 = 'bc'
reg.test(str1) // false  ？means one maxiumum
reg.test(str2) // true
```
• Reg.compile(str) Compiles regular expressions for easy multiple calls and improves performance. Refer to the rookie tutorial:This method can compile the specified regular expression, and the execution speed of the regular expression after compilation will increase. If the regular expression is called multiple times, then calling the compile method can effectively improve the execution speed of the code. If the regular expression is only Can be used once, there will be no obvious effect.
```  var str="Every man in the world! Every woman on earth!";
patt=/man/g;
str2=str.replace(patt,"person");
console.log(str2); // Every person in the world! Every woperson on earth!

patt=/(wo)?man/g;
patt.compile(patt);
str2=str.replace(patt,"person");
console.log(str2); // Every person in the world! Every person on earth!
```
• Reg.exec(str) takes a string and returns an array, otherwise returns null.
The return value is an array interpretation:
1 If there are multiple matches
1 The 0th element of this array is the text that matches the regular expression.
2 The first element is the text that matches the first subexpression of reg, if any.
3 The second element is the text that matches the second subexpression of reg, if any.
4…
2 index Matches the index of the first character of the text
3 input is the retrieved string str

```  var str = '2018ceshi2019'
var reg = /(\d)(\w)/;
var result = reg.exec(str)
console.log(result);//["20", "2", "0", index: 0, input: "2018ceshi2019"]
```
```  var str = 'The Quick Brown Fox Jumps Over The Lazy Dog'
var reg = /quick\s(brown).+?(jumps)/ig;
var result = reg.exec(str);
console.log(result); //  ["Quick Brown Fox Jumps", "Brown", "Jumps", index: 4, input: "The Quick Brown Fox Jumps Over The Lazy Dog", groups: undefined]
// result [0] Match all strings 'Quick Brown Fox Jumps'
//        [1],[2],...[n] Group capture in parentheses [1] = Brown [2] = Jumps
//        index :The matched character is located in the zero-based index value of the original string 4
//        input : Original string The Quick Brown Fox Jumps Over The Lazy Dog
```

ps:If you don’t need to capture the content of the distribution, you don’t need to add ?: in the group capture, which means the match is not captured.。
var reg = /quick\s(?:brown).+?(?;jumps)/ig; This will not capture the grouping of [1], [2],…[n].

• Str.match(reg) searches the string for content that matches the rule. If successful, it returns the contents of the array. If it fails, it returns null.
```  var str = 'abc123def45g6hijkl789';
var reg1 = /\d+/g;   // Match at least one number at a time and match globally
console.log( str.match(reg1));   // [123，45，6，789]
var reg2 = /\d+/;
console.log(str.match(reg2)); // ["123", index: 3, input: "abc123def45g6hijkl789", groups: undefined]
var reg3 = /\d/;
console.log(str.match(reg3));//["1", index: 3, input: "abc123def45g6hijkl789", groups: undefined]
var reg4 = /\d/g;
console.log(str.match(reg4)) // ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
```

From these four examples, the global match, the result is an array, if it is not a global match, when the number 123 is found, it will stop. It will only pop up 123, similar to the result with exec(). Plus the global match, it will search for the rules from the beginning to the end. If there is no plus sign, the result of the match is [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”], with plus No. The number that matches each time is at least one.

• Str.search(reg) searches the string for an index that matches the regular content, or -1 if it does not exist.
```  var str = 'abcdefg';
var reg1 = /B/i;
console.log(str.search(reg1)) // 1
var reg2 = /B/;
console.log(str.search(reg2)) // -1
```
• Str.replace (reg, new str / callback)
parameter interpretation: The first parameter reg: the regular content to be matched, the
second parameter new str / callback. Callback, the callback function also has three parameters: 1 matching character, 2 matching character index, 3 source string.
• 1 Find the maximum number of characters in the duplicate
```  var str = 'aibbicidhdieifigbdihdaii';
var arr = str.split(''); //Convert a string to an array
str = arr.sort().join(''); //Sort first, so the result will put the same characters together and then convert to a string
console.log(str);  // aabbbcddddefghhiiiiiiiii
var value = '';
var index = 0;
var re = /(\w)\1+/g;  //Match characters, repeat this character, repeat at least once。
str.replace(re,function(\$0,\$1){
console.log(\$0);   //Represents the result of each successful match : aa dd jj kk l sssssssssssssssss
console.log(\$1);  //Represents the first child of each successful match, that is\w:  a d j k l S
if(index<\$0.length){  //If the value saved by index is less than the length of \$0, proceed as follows
index = \$0.length;  // This way the index is always saved at the maximum length.
value = \$1;  //Value holds the most occurrence of this character
}

});
console.log('Most characters:'+value+',Number of repetitions:'+index);  // The most characters: i, the number of repetitions: 9
```
• Uppercase numbers replace lowercase numbers
```  var str = "2019";
var a = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
str = str.replace(/\d/g, function () {
return a[arguments[0]];
});
console.log(str);//贰零壹玖
```