JavaScript regexp object (regular expression)

Time:2021-7-28

Use the explicit constructor of regexp. The syntax is: new regexp (“pattern” [, “flags”]).
Use the implicit constructor of regexp in plain text format: / pattern / [flags].
The pattern part is the regular expression pattern text to be used, which is required. In the first method, the pattern part exists as a JavaScript string and needs to be enclosed in double quotation marks or single quotation marks; In the second way, the pattern part is nested between two “/”, and quotation marks cannot be used.
The flags section sets the flag information of the regular expression. It is optional. If the flags part is set, in the first mode, it exists in the form of string; In the second way, it is in the form of text immediately after the last “/” character. Flags can be a combination of the following flag characters.
G is the global flag. If this flag is set, when searching and replacing a text, it will work on all matching parts of the text. If this flag is not set, only the earliest matching content is searched and replaced.
I is the ignore case flag. If this flag is set, case will be ignored during matching comparison.
M is a multiline flag. If this flag is not set, the metacharacter “^” only matches the start position of the entire searched string, and the metacharacter “$” only matches the end position of the searched string. If this flag is set, “^” can also match the position after “\ n” or “\ R” in the searched string (i.e. the beginning of the next line), and “$” can also match the position after “\ n” or “\ R” in the searched string (i.e. the end of the next line).
Code 8.1 is an example of creating a regular expression.
Code 8.1 creating regular expressions: 8.1.htm


[Ctrl + a full selection note: you need to refresh the page to import external JS]

The running result of the above code is shown in Figure 8.1.
Since “\” in JavaScript string is an escape character, when creating regexp instance object using explicit constructor, replace “\” in original regular expression with “\ \”. For example, the two statements in code 8.2 are equivalent.
Code 8.2 “\” in escape character: 8.2.htm
<script language=”javascript“>
var re1 = new RegExp(“\\d{5}”);
var re2 = /\d{5}/;
alert(“re1=”+re1+”\nre2=”+re2);
</script>
The program display results are shown in Figure 8.2. It can be seen that the two results are the same.
Since the escape character in the regular expression pattern text is also “\”, if the regular expression is to match the original character “\”, it should be represented by “\ \” in the regular expression pattern text. When the regexp instance object is created by using the explicit constructor, it is necessary to use “\ \ \ \” to represent the original character “\”.
var re = new RegExp(\\\\)。
8.4 properties of regexp object
The properties of regexp objects are divided into static properties and instance properties. The following are introduced respectively.
8.4.1 static attributes
(1) Index attribute. Is the starting position of the first matching content of the current expression pattern, counting from 0. The initial value is – 1. The index attribute will change each time a successful match is made.
(2) Input attribute. Returns the current string, which can be abbreviated as $, The initial value is an empty string ”.
(3) Lastindex property. It is the next position of the last character in the first matching content of the current expression pattern. It is counted from 0. It is often used as the starting position when continuing the search. The initial value is – 1, indicating that the search starts from the starting position. The lastindex attribute value will change each time the matching is successful.
(4) Lastmatch property. Is the last matching string of the current expression pattern, which can be abbreviated as $&. Its initial value is the empty string ”. The lastmatch attribute value changes each time a match is successful.
(5) Lastparen property. If there are sub matches enclosed in the expression pattern, it is the sub string matched by the last sub match in the current expression pattern, which can be abbreviated as $+. Its initial value is the empty string ”. Each time a successful match occurs, the lastparen attribute value changes.
(6) Leftcontext property. Is all the contents to the left of the last matching string in the current expression pattern, which can be abbreviated as $` (where “‘” is the anti single quotation mark under “ESC” on the keyboard). The initial value is an empty string ”. Each time a match is successful, its attribute value changes.
(7) Rightcontext property. Is everything on the right of the last matching string in the current expression pattern, which can be abbreviated as $’. The initial value is an empty string ”. Each time a match is successful, its attribute value changes.
(8) $1… $9 attribute. These properties are read-only. If there are enclosed sub matches in the expression pattern, $1… $9 attribute values are the contents captured by the 1st to 9th sub matches respectively. If there are more than 9 sub matches, $1… $9 attributes correspond to the last 9 sub matches respectively. In an expression pattern, you can specify any number of bracketed sub matches, but the regexp object can only store the results of the last 9 sub matches. In the result array returned by some methods of the regexp instance object, you can get all the sub matching results in parentheses.
8.4.2 instance properties
(1) Global attribute. Returns the status of the global flag (g) specified when creating a regexp object instance. If the G flag is set when creating the regexp object instance, the property returns true; otherwise, it returns false, and the default value is false.
(2) Ignorecase property. Returns the status of the ignorecase flag (I) specified when creating a regexp object instance. If the I flag is set when creating the regexp object instance, the property returns true; otherwise, it returns false, and the default value is false.
(3) Multiline property. Returns the status of the multiline flag (m) specified when the regexp object instance was created. If the m flag is set when creating the regexp object instance, the property returns true; otherwise, it returns false, and the default value is false.
(4) Source property. Returns the expression text string specified when creating a regexp object instance.
8.5 method of regexp object
The common methods of regexp objects are test, exec and compile. This section describes the functions and usage of these methods. Finally, a comprehensive example of the properties and methods of regexp object is given.
8.5.1 test method
The syntax format is test (STR). This method checks whether the expression mode specified when creating regexp object instance exists in a string. If it exists, it returns true; otherwise, it returns false. If a match is found, the relevant static properties in the regexp object are updated to reflect the match. As for the use of this method, it will be often used in the following section 8.10, and there is no separate example here.
Exec method
The syntax format is exec (STR). This method searches a string using the expression pattern specified when creating the regexp object instance, and returns an array containing the search results.
If the global flag (g) is set for a regular expression, you can continuously search the string by calling the exec and test methods multiple times, each time starting from the position specified by the lastindex attribute value of the regexp object.
If the global flag (g) is not set, the exec and test methods ignore the lastindex property value of the regexp object and start the search from the beginning of the string.
If the exec method does not find a match, the return value is null; If a match is found, an array is returned and the static properties in the regexp object are updated to reflect the match. The element 0 in the returned array contains the complete matching result, and the elements 1 ~ n are the results of each sub matching defined in the expression pattern in turn.
The array returned by the exec method has three properties: input, index and lastindex.
The input property is the entire searched string.
The index property refers to the position of the match in the entire searched string.
The lastindex property refers to the next character position of the last character of the matched substring.
Code 8.3 is an example of the application of this method.
Code 8.3 exec() method application: 8.3.htm


[Ctrl + a full selection note: you need to refresh the page to import external JS]

The running result of the above code is shown in Figure 8.3.
Figure 8.3 operation results
8.5.3 compile method
The syntax format is compile (“pattern” [, “flags”]). This method can replace the expression pattern used by the regexp object instance and compile the new expression pattern into the internal format, so as to make the subsequent matching process faster. If you want to reuse an expression in a loop, compiling it will speed up execution. However, if you use any other expression pattern in your program and then use the originally compiled expression pattern, this compilation is of no benefit

Recommended Today

Kubernetes core practice (VI) — configmap

8、ConfigMap Extract the application configuration and update it automatically create profile [[email protected] ~/yaml/test]# vim configmap.yaml [[email protected] ~/yaml/test]# cat configmap.yaml apiVersion: v1 data: redis.conf: | appendonly yes kind: ConfigMap metadata: name: redis-conf namespace: default [[email protected] ~/yaml/test]# [[email protected] ~/yaml/test]# kubectl apply -f configmap.yaml configmap/redis-conf created [[email protected] ~/yaml/test]# View configuration [[email protected] ~/yaml/test]# kubectl get configmaps NAME DATA AGE […]