Js Regular Expression learning notes,
Regular Expression:Search, replace, and extract information in a string. (Comments and blank spaces are not supported and must be written in one line)
Create a regular expression:Characters that are included in a pair of slashes (direct amount syntax)
For example:
Var pattern =/s $/; // create a regular expression to match all strings ending with the letter s and assign it to pattern.
I. character classes
Put the character directly in square brackets to form a character class.
A character class can match any character it contains.
Character classes of regular expressions:
[...]Any character in square brackets
[^...]Any character not in square brackets
.Any character except the line break and other Unicode line terminator
\ WAny word consisting of ASCII characters is equivalent to a [a-zA-Z0-9]
\ WAny word consisting of non-ASCII characters is equivalent to a [^ a-zA-Z0-9]
\ SAny Unicode white space character
\ SAny non-Unicode whitespace
\ DAny ASCII number is equivalent to [0-9]
\ DAny non-ASCII number is equivalent to [^ 0-9]
[\ B]Direct unspace (Special Case)
For example:/[A, B, c]/matches any one of the letters a, B, and c
/[\ S \ d]/indicates matching any blank character or number
Ii. Repetition
Mark used to indicate repeated characters
Repeated character Syntax of a regular expression:
{N, m}Match the first item at least n times, but not more than m times
{N,} matches the previous item n times or more times
{N} matches the previous item n times
? Match the first item 0 or 1, that is, the first item is optional, equivalent to {0, 1}
+ Match the previous item once or multiple times, equivalent to {1 ,}
* Match the first item 0 or multiple times, equivalent to {0 ,}
For example:/\ D {2, 4}/match 2 ~ 4 digits
/\ W {3} \ d/exact match of 3 words and an optional number
3. Selection, grouping, and reference
Matching program: from left to right. If the matching item on the left matches, the matching item on the right is automatically ignored (even if a better match can be generated)
① | Used to separate selected characters
For example,/AB | cd | ef/can match the string "AB", "cd", or "ef"
/\ D {3} | [a-z] {4}/matches three digits or four lower-case letters
② ()
Role 1:Synthesize a separate item into a subexpression
Role 2:Define the sub-mode in the complete Mode
When a regular expression is successfully matched with the target string, you can extract the part that matches the child pattern in parentheses from the target string.
For example:
/[A-z] + \ d +/retrieve one or more lower-case letters followed by one or more numbers
If we care about the numbers at the end of each model, we can place the numeric part of the model in parentheses (/[a-z] + (\ d )/), in this way, you can extract numbers from the retrieved matching.
Role 3:Allows you to introduce the previous subexpression at the back of the same regular expression.
Implementation Method: add one or multiple-digit characters after the character \ (This number specifies the position of the subexpression with parentheses in the regular expression)
For example, \ 1 references the first subexpression with parentheses.
Note: The positions are subject to the left brackets involved in the count;
Regular Expressions Do not allow single quotes in the content enclosed by double quotes, and vice versa.
Summary:
|Select to match the child expression on the left of the character or the child expression on the right.
(...) Combination, combining several items into a unit
(...?) Only combines items into one unit, but does not remember the characters matching the group.
\ N matches the characters that match the nth group for the first time. The Group is a subexpression in parentheses, and the group index is the number of left parentheses from left to right ,(? : Groups in this form do not participate in index encoding.
4. Specify the matched position:
Anchor:Specify a valid location for matching
^Match the start of a string
$Matches the end of a string
\ BMatch the boundary of a word (between \ w and \ W)
\ BMatch the position of a non-word boundary
5. Modifier
The modifier is placed out of // and does not appear between two/lines.
ICase Insensitive
GGlobal match: locate all matches, instead of stopping after finding the first one.
MMulti-row matching
6. Method of String object used for Pattern Matching
Method 1:Matched position of search ()
Parameter: A Regular Expression
Return: the starting position of the first matched substring. If no matched substring is found,-1 is returned.
For example:
<Script> "javascript". search (/script/I); // The returned value is 4. </script>
Note: ① if the search () parameter is not a regular expression, it will be converted to a regular expression through the RegExp Constructor
② Search () does not support global search
Method 2:Replace () for retrieval and replacement
Parameter: First --> Regular Expression
The second --> string to be replaced (also a function that dynamically calculates the replacement string)
Note: ① supports g,
G is set in the regular expression: All substrings matching the pattern will be replaced with the string specified by the second parameter.
G is not set in the regular expression: only replace the matched first substring
② If the first parameter is not a regular expression but a string, replace () will directly search for this string.
For example, replace all case-insensitive javascript with the JavaScript code that is in uppercase or lowercase.
<script> var str = "javascript,javascript,javascript,javaScript"; alert(str.replace(/javascript/gi,"JavaScript")); //JavaScript,JavaScript,JavaScript,JavaScript</script>
Method 3:Match ()
Parameter: Regular Expression
Return: An array composed of matching results
Supports g
Instance 1:
<script> var math = "1 plus 2 equals 3".match(/\d+/g); console.log(math); // ["1", "2", "3"] console.log(typeof math); //object</script>
Example 2:
<script> var math = "1 plus 2 equals 3".match(/\d/); console.log(math); // ["1", index: 0, input: "1 plus 2 equals 3"] console.log(typeof math); //object</script>
Note:
Input a non-global regular expression to the math () of the string:
Matching: only the first match is retrieved.
Returned value: Array
First element of the array: matched string
The second element of the array: a subexpression enclosed in parentheses in the regular expression. The returned array has two attributes:> index and input.
Method 4:Split ()
Returned value: Array
Delimiter: parameters of split ()
For example:
<script> var str= "1,2,3,4,5,6,7,8".split(','); console.log(str); //["1", "2", "3", "4", "5", "6", "7", "8"]</script>
When the parameter is a regular expression (you can specify a separator to allow any margin characters on both sides)
Return: the string used to call it is split into an array consisting of sub-strings
<Script> var str = "1, 2, 4, 5, 6, 7, 8 ". split (/\ s *, \ s */); // allows the console to be left with any margin on both sides. log (str); // ["1", "2", "3", "4", "5", "6", "7 ", "8"] </script>
VII. RegExp object
RegExp () constructor:
Parameter: Two string Parameters
First: the body of the regular expression (text between two slashes)
Second: (optional) modifier of the regular expression (g, I, m, or a combination of the three)
Note: whether it is a string or a regular expression, the/character is used as the prefix of the translated character.
For example:
Var zipCode = new RegExp ('\ d {5}', 'G'); // globally match the five numbers in the string. Note that the value here is // instead/
The above are the notes for learning about js regular expressions. I hope this will help you learn js regular expressions.
Articles you may be interested in:
- Javascript mobile phone number Regular Expression verification function
- Proficient in JS Regular Expressions (recommended)
- Detailed explanation of the js Code of the regular expression in the verified email address
- Explanation of the match function of js Regular Expressions
- Test function for js Regular Expressions
- Practical JS Regular Expressions (mobile phone number/IP address Regular Expression/zip code Regular Expression/phone number)
- Use of js Regular Expressions
- JS Regular Expressions (detailed and practical)
- Verify the digital code using the JS Regular Expression