Regular expression:Implements search, replace and extract information in strings. (No comments and blanks are supported, they must be written in one line)
Creation of regular expressions:Characters contained between a pair of slashes (direct quantity syntax)
For example:
var pattern = /s$/; // Create a regular to match all strings ending with letters and assign them to pattern
1. Character Class
Putting the direct measurement characters into square brackets alone forms a character class.
A character class can match any character it contains.
Character class for regular expressions:
[...] Any character in square brackets
[^...] Any character not in square brackets
. Any character except line breaks and other Unicode line terminators
\w Any word composed of ASCII characters is equivalent to [a-zA-Z0-9]
\W Any word composed of non-ASCII characters is equivalent to [^a-zA-Z0-9]
\s Any Unicode blank sign
\S Any non-Unicode blanks
\d Any ASCII number is equivalent to [0-9]
\D Any non-ASCII number is equivalent to [^0-9]
[\b] Backspace direct quantity (special case)
For example:/[a,b,c]/
/[\s\d]/ �
2. Repeat
A mark to represent duplicates of specified characters
Repeat character syntax for regular expressions:
{n,m} Match the previous item at least n times, but not more than m times
{n,} Match the previous item n times or more
{n} Match the previous item n times
? Match the previous item 0 or 1 time, that is, the previous item is optional, equivalent to {0,1}
+ Match the previous item 1 or more times, equivalent to {1,}
* Match the previous item 0 or more times, equivalent to {0,}
For example:/\d{2,4}/ Match 2~4 numbers
/\w{3}\d/ Exactly match 3 words and an optional number
3. Selection, Grouping and Reference
Matching program: From left to right, if the left match match matches, the right match will be automatically ignored (even if a better match can be produced)
① | Used to separate the characters for selection
For example: /ab|cd|ef/ It can match the strings "ab", "cd" or "ef"
/\d{3}|[a-z]{4}/ Match 3 digits or four lowercase letters
② ()
Function 1:Synthesize individual items into subexpressions
Function 2:Define sub-modes in full mode
When a regular expression successfully matches the target string, the part that matches the subpattern in parentheses can be extracted from the target string.
For example:
/[a-z]+\d+/ Search for one or more lowercase letters followed by one or more numbers
If we are concerned about the numbers at each tail, we can put the numeric part of the pattern in brackets (/[a-z]+(\d)/), so that we can extract the numbers from the retrieved match.
Function 3:Allows the introduction of the previous subexpression behind the same regular expression
Implementation method: add one or more digits after the character\ (this number specifies the position of the subexpression with parentheses in the regular expression)
For example: \1 Reference is the first subexpression with parentheses
Note: The position shall be based on the left bracket position participating in the count;
Regular expressions do not allow single quotes in double quotes, and vice versa.
Summarize:
| Select, the subexpression on the left or subexpression on the right of the character is matched.
(...) Combination, combine several items into one unit
(...?) Combination only, combine items into a unit, but do not remember characters matching the group
\n Matches the character that matches the first time the nth group matches. The group is a subexpression in parentheses. The group index is a number of left brackets from left to right. (?: This form of grouping does not participate in index encoding.
4. Specify the matching location:
Anchor:Specify the legal location where the match occurs
^ Match the beginning of the string
$ Match the end of the string
\b Match the boundary of a word (located between \w and \w)
\B Match the position of non-word boundaries
V. Modifier
The modifier is placed outside // and will not appear between two / lines.
i Case insensitive
g Global match, find all matches, instead of stopping after finding the first one
m Multi-line matching
6. Methods for pattern matching String objects
Method 1:search() Search the matching location
Parameters: a regular expression
Return: The start position of the first substring that matches it. If the matching substring cannot be found, return -1
For example:
<script> "javascript".search(/script/i); //The return value is 4</script>
Note: ① If the parameter of search() is not a regular expression, it will be converted into a regular expression through the RegExp constructor.
② Search() does not support global search
Method 2:replace() performs search and replace operations
Parameters: First --> Regular Expression
The second --> The string to be replaced (also a function, which can dynamically calculate the replacement string)
Note: ① Support g,
G is set in the regular expression: all substrings that match the pattern will be replaced with the string specified by the second parameter.
G is not set in the regular expression: only the first substring that matches is replaced
② If the first parameter is not a regular but a string, replace() will directly search for the string.
For example: replace all case-insensitive javascript with correct case-correct JavaScript
<script> var str = "javascript,javascript,javascript,javaScript"; alert((/javascript/gi,"JavaScript")); //JavaScript,JavaScript,JavaScript,JavaScript </script>
Method 3: match()
Parameters: Regular expression
Return: an array of matching results
Support g
Example 1:
<script> var math = "1 plus 2 equals 3".match(/\d+/g); (math); // ["1", "2", "3"] (typeof math); //object </script>
Example 2:
<script> var math = "1 plus 2 equals 3".match(/\d/); (math); // ["1", index: 0, input: "1 plus 2 equals 3"] (typeof math); //object </script>
Notice:
Pass a non-global regular expression to the math() of the string:
Matching: Only the first match is retrieved
Return value: array
The first element of the array: the matching string
The second element of the array: a subexpression enclosed in parentheses in the regular expression. The returned array has two properties -> index and input
Method 4:split()
Return value: array
Delimiter: Parameters of split()
For example:
<script> var str= "1,2,3,4,5,6,7,8".split(','); (str); //["1", "2", "3", "4", "5", "6", "7", "8"] </script>
When the parameter is a regular expression (the separator can be specified, allowing as many whitespace characters on both sides)
Return: Split the string to call it into an array composed of a substring
<script> var str= "1,2,3,4,5,6,7,8".split(/\s*,\s*/); //Allow as many blank signs on both sides (str); //["1", "2", "3", "4", "5", "6", "7", "8"] </script>
7. RegExp object
RegExp() constructor:
Parameters: Two string parameters
The first: The body part of the regular expression (text between two slashes)
The second: (optional) specifies the modifier of the regular expression (g, i, m or a combination of these three)
Note: Whether it is a direct string or a regular expression, use /char as the prefix for the translated characters.
For example:
var zipCode = new RegExp('\\d{5}','g'); //Globally match the string5Numbers,Note that this is//Instead/
The above are the learning notes about js regular expressions compiled for you. I hope it will be helpful for you to learn js regular expressions.