Matching mode:
Regular expressions match patterns, such as the g in /\d+/g used below.
- G: Global mode, which looks for the entire content of the string instead of ending with the first match.
- I: Case insensitive, which means that the case of pattern and string is ignored when searching for matches.
- M: Multi-line mode, which indicates that the search will continue until the end of a line of text.
- Y: Adhesion mode, which only looks for strings starting from and after lastIndex.
- U: Unicode mode, enabling Unicode matching.
- S: indicates the metacharacter in dotAll mode. Matches any character (including \n or \r).
1. Literal creation
2. Constructor creation
//1. Create a literal
let str = "abc1234dfa123";
let reg = /\d+/g;
let res = str.match(reg);
console.log(res);
//2. Create a constructor
let str = "abc1234dfa123";
let reg = new RegExp("\\d+"."g");
let res = str.match(reg);
console.log(res);
Copy the code
3. Method of regeting objects
-
Test returns a Boolean value indicating whether a match can be found
-
Exec returns matching substrings, one at a time in non-sticky mode
// The method under the re object
// 1.test returns a Boolean value indicating whether a match can be found
let str = "abfda";
let reg = /\d+/g;
console.log(reg.test(str));
// 2.exec returns matching substrings, one at a time
let str = "abf1234da123";
let reg = /\d+/g;
console.log(reg.exec(str));
console.log(reg.lastIndex);
console.log(reg.exec(str));
console.log(reg.lastIndex);
console.log(reg.exec(str));
Copy the code
4. String methods
- Split separates strings according to regular rules
- Search returns the position matching the default first element, not globally
- Match Performs a regular match and returns the match result
- Replace Replaces the matched value
// String methods
// 1.split splits the string according to the regular rule
let str = "asfhk121asdad123";
let arr = str.split(1);
let arr2 = str.split(/\d+/);
console.log(arr, arr2);
//2.search search returns the position matching the default first element, not globally
let str = "safk123asdak123";
let reg = /\d+/;
let reg2 = /\d+/g;
let arr = str.search(reg);
let arr2 = str.search(reg2);
console.log(arr, arr2);
//3. Match Performs a regular match and returns the matching result
let str = "sadsadjhk213asdf1312jfh";
let reg = /\d+/g;
console.log(str.match(reg));
//4.replace Replaces the matched value
let str = 'asdkj12321sadjk213';
let arr = str.replace(/\d+/g."*");
console.log(arr);
let arr2 = str.replace(/\d+/g.function(arg) {
console.log(arg);
return '* *'
})
console.log(arr2);
Copy the code
5. Yuan character
1. Character correlation 2. Quantity correlation 3. Position correlation 4
-
Character correlation, character correlation is    \w & NBSP \w & NBSP \d & NBSP \d & NBSP \s & NBSP \s. & NBSP case works in reverse.
\ W: numbers, letters, underscores;    \W: not (numbers, letters, underscores)
\ D: numbers;    \D: non-numeric
\s: space;   \ S: non-space
. Means not \n \r \u2028 \u2029;
// \w: numbers, letters, underscores \W: Not (numbers, letters, underscores)
let str = '123fdfs# ###%%%__';
let reg = /\w+/g;
console.log(str.match(reg));
let reg2 = /\W+/g;
console.log(str.match(reg2));
// \d: digit \d: non-digit
let str = '123fdfs# ###%%%__';
let reg = /\d+/g;
console.log(str.match(reg));
let reg2 = /\D+/g;
console.log(str.match(reg2));
// \s: space \s: non-space
let str = '123fdfs# ###%%%__';
let reg = /\s+/g;
console.log(str.match(reg));
let reg2 = /\S+/g;
console.log(str.match(reg2));
//. Not \n \r \u2028 \u2029;
let str = '123fdfs# ###%%%__';
let reg = /./g;
console.log(str.match(reg));
Copy the code
-
**{}? + * * *
-
First look at **{}**, {} is divided into three kinds
One is a fixed value, such as {3}.
Two is the specified range, for example {2,5}.
Three is the specified minimum, for example {1,}
-
let str = "abceeeeeeeffd";
let reg = /ce{3}/g; / / 1. A fixed value
console.log(str.match(reg));
let reg2 = / ce/g {1, 4}; //2. Specify a range. Default is greedy matching
console.log(str.match(reg2));
let reg22 = / ce {2, 4}? /g; // Non-greedy match
console.log(str.match(reg22));
let reg3 = /ce{1,}/g; //3. Specify the minimum value. Default is greedy matching
console.log(str.match(reg3));
let reg33 = /ce{1,}? /g; // Non-greedy match
console.log(str.match(reg33));
Copy the code
-? Equivalent to {0, 1} - + equivalent to {1,} - * equivalent to {0,}Copy the code
**^ \b\ b ∗∗ Indicates the start of a character, \b \b **. ^ Indicates the start of a character, \b\ b ∗∗. Indicates the beginning of a character, indicates the end of a character, \b indicates a boundary character, \b indicates a non-boundary character.
^ $\b \b
// ^ Indicates the start of a character
let str = "abefsdsfsd";
let reg = /^/g;
console.log(str.replace(reg, "*"));
// $indicates the end of a character
let str = "abefsdsfsd";
let reg = /$/g;
console.log(str.replace(reg, "*"));
// /b represents the boundary character, and non-\ w is the boundary character
let str = "my name is book";
let reg = /\bis\b/g;
console.log(str.replace(reg, "* *"));
// /B means non-boundary
let str = "my name is book";
let reg = /\Bok\b/g;
console.log(str.replace(reg, "* *"));
Copy the code
- The parentheses are related. () [] {} where **{}** has been mentioned in the previous
- () has four functions, namely grouping, extracting value, replacing and backreferencing
- Grouping, in fact, I think is a kind of priority
/ / group
let str = "dkdkdk2311234";
let reg = /(dk){3}/g;
console.log(str.match(reg));
Copy the code
- Extract the value
/ / value
let str = "The 2020-10-03-04 - '05";
let reg = /(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})/;
console.log(str.match(reg));
// //RegExp can store matches for up to nine capture groups. These attributes are accessed through RegExp.$1 to RegExp.$9 and contain matches for the 1st to 9th capture groups, respectively
console.log(RegExp. $1);console.log(RegExp. $2);console.log(RegExp. $3);console.log(RegExp$4);console.log(RegExp. $5);Copy the code
A quick tip: RegExp can store matches for up to nine capture groups. These attributes are accessed through RegExp.$1 to RegExp.$9 and contain matches for the 1st to 9th capture groups, respectivelyCopy the code
- replace
/ / replace
let str = "2020-10-01";
let reg = /(\d{4})-(\d{2})-(\d{2})/;
console.log(str.replace(reg, "$3 / $2 / $1"));
console.log(str.replace(reg, (arg, year, mounth, date) = > {
return date + '/' + mounth + '/' + year;
}));
Copy the code
- backreferences
// backreference
let className = "news-container-nav";
// let reg = /\w{4}(-|_)\w{9}(-|_)\w{3}/;
let reg = /\w{4}(-|_)\w{9}(\1)\w{3}/;
console.log(className.match(reg));
Copy the code
[] is a simple set of characters. Note that the ^ character in [] means not
// [] is a collection of characters. ^ in [] means not
let str = 'My name is liUuuudk';
// let reg = /li[Uu]+dk/g;
let reg = /[^0-9]/g;
console.log(str.match(reg));
Copy the code
5. Name groups
// Name the group
let str = "2020-10-01";
let reg = / (?
\d{4})-(?
\d{2})-(?
\d{2})/
;
console.log(str.match(reg));// Open the console and you can see the values in groups
Copy the code
6. Zero-width assertion
- Forward zero-width assertion
- sure
// Forward zero-width assertion
// Can be divided into positive and negative
let str = "iphone2iphone3iphone4iphonenumber";
/ / sure
let reg1 = /iphone(? =\d)/g; // When the iPhone is followed by a number
console.log(str.replace(reg1, The word "apple"));
Copy the code
- noCopy the code
/ / no
let reg2 = /iphone(? ! \d)/g; //// when iPhone is not a number
console.log(str.replace(reg2, The word "apple"));
Copy the code
- Reverse zero-width assertion
- sure
// Reverse zero-width assertion
// Can be divided into positive and negative
let str = '10px20px30pxsspx';
/ / sure
let reg1 = / (? <=\d+)px/g;
console.log(str.replace(reg1, "Pixel"));
Copy the code
- noCopy the code
/ / no
let reg2 = / (?
;
console.log(str.replace(reg2, "Pixel"));
Copy the code