Here are some examples of regular expressions you might encounter:
/^\[ \t]*$/ "^\[ \t]*$" Matches a blank line.
/\d{2}-\d{5}/ "\d{2}-\d{5}" Verify whether an ID number consists of a 2-digit word, a hyphen and a 5-digit number.
/<(.*)>.*<\/\1>/ "<(.*)>.*<\/\1>" Matches an HTML tag.
The following table is a complete list of metacharacters and their behavior in the context of regular expressions:
Character Description
\ Mark the next character as a special character, or an primitive character, or a backward reference, or an octal escape character. For example, 'n' match the character "n". '\n' matches a newline character. The sequence '\\' matched "\" and "\(" matched "(".
^ Matches the start position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after '\n' or '\r'.
$ Match the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the previous position of '\n' or '\r'.
* Match the previous subexpression zero or multiple times. For example, zo* can match "z" and "zoo". * Equivalent to {0,}.
+ Match the previous subexpression once or more times. For example, 'zo+' can match "zo" and "zoo", but cannot match "z". + equivalent to {1,}.
? Match the previous subexpression zero or once. For example, "do(es)?" can match "do" in "do" or "does". ? Equivalent to {0,1}.
{n} n is a non-negative integer. Match n times that are determined. For example, 'o{2}' cannot match 'o' in "Bob" , but can match 'o' in "food" .
{n,} n is a non-negative integer. Match at least n times. For example, 'o{2,}' cannot match 'o' in "Bob" , but can match all o in "fooooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.
{n,m} m and n are both non-negative integers, where n <= m. Match at least n and match at most m. Liu, "o{1,3}" will match the first three os in "foooooood". 'o{0,1}' is equivalent to 'o?'. Please note that there cannot be spaces between commas and two numbers.
? When the character is immediately followed by any other restriction character (*, +, ?, {n}, {n,}, {n,m}) , the matching pattern is non-greedy. The non-greedy pattern matches as few strings as possible, while the default greedy pattern matches as many strings as possible. For example, for the string "oooo", 'o+?' will match a single "o", and 'o+' will match all 'o'.
. Match any single character except "\n". To match anything including '\n'
For any character, please use the pattern like '[.\n]'.
(pattern) Match pattern and get this match. The obtained matches can be obtained from the generated Matches collection, using the SubMatches collection in VBScript, and the $0…$9 attribute in Visual Basic Scripting Edition. To match parentheses characters, use '\(' or '\)'.
(?:pattern) Match pattern but does not get the matching result, which means that this is a non-get match and is not stored for future use. This is useful when using the "or" character (|) to combine various parts of a pattern. For example, 'industr(?:y|ies) is an expression that is simpler than 'industry|industries'.
(?=pattern) Forward pre-check, match the search string at the beginning of any string matching pattern . This is a non-get match, that is, the match does not need to be retrieved for later use. For example, 'Windows (?=95|98|NT|2000)' can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3 .1".
Pre-checking does not consume characters, that is, after a match occurs, the next match's search begins immediately after the last match, rather than after the characters containing the pre-checking.
(?!pattern) Negative pre-check, match the search string at the beginning of any string that does not match Negative lookahead matches the search string at any point where a string not matching pattern . This is a non-get match, that is, the match does not need to be retrieved for later use. For example, 'Windows (?!95|98|NT|2000)' can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000". Pre-checking does not consume characters, that is, after a match occurs, the next match search begins immediately after the last match, rather than starting after the characters containing the pre-checking.
x|y match x or y. For example, 'z|food' can match "z" or "food". '(z|f) ood' matches "zood" or "food".
[xyz] Character collection. Match any character contained. For example, '[abc]' can match 'a' in "plain" .
[^xyz] The set of negative values characters. Match any characters not included. For example, '[^abc]' can match 'p' in "plain" .
[a-z] Character range. Match any character in the specified range. For example, '[a-z]' can match any lowercase alphabetical characters in the range 'a' to 'z'.
[^a-z] Negative value character range. Match any arbitrary characters that are not within the specified range. For example, '[^a-z]' can match any arbitrary character that is not in the range of 'a' to 'z'.
\b Match a word boundary, which means the position of the word and space. For example, 'er\b' can match 'er' in "never" , but cannot match 'er' in "verb".
\B Match non-word boundaries. 'er\B' can match 'er' in "verb" , but cannot match 'er' in "never".
\cx Matches the control characters specified by x. For example, \cM matches a Control-M or carriage return. The value of x must be one of A-Z or a-z. Otherwise, treat c as an original 'c' character.
\d Match a numeric character. Equivalent to [0-9].
\D Match a non-numeric character. Equivalent to [^0-9].
\f Match a page break. Equivalent to \x0c and \cL.
\n Match a newline character. Equivalent to \x0a and \cJ.
\r Match a carriage return character. Equivalent to \x0d and \cM.
\s Match any whitespace characters, including spaces, tabs, page breaks, etc. Equivalent to [ \f\n\r\t\v].
\S Match any non-whitespace characters. Equivalent to [^ \f\n\r\t\v].
\t Match a tab character. Equivalent to \x09 and \cI.
\v Match a vertical tab character. Equivalent to \x0b and \cK.
\w Match any word character that includes an underscore. Equivalent to '[A-Za-z0-9_]'.
\W Match any non-word character. Equivalent to '[^A-Za-z0-9_]'.
\xn Match n, where n is a hexadecimal escape value. The hexadecimal escape value must be the length of two numbers that are determined. For example, '\x41' matched "A". '\x041' is equivalent to '\x04' & "1". ASCII encoding can be used in regular expressions. .
\num Match num, where num is a positive integer. Reference to the obtained match. For example, '(.)\1' matches two consecutive identical characters.
\n Identifies an octal escape value or a backward reference. If \n before at least n retrieved subexpressions, then n is a backward reference. Otherwise, if n is an octal number (0-7), then n is an octal escape value.
\nm Identifies an octal escape value or a backward reference. If there are at least is preceded by at least nm retrieval subexpressions before, then nm is a backward reference. If there are at least n retrieves before \nm , then n is a hesitation text
Backward reference of word m. If the previous conditions are not satisfied, if both n and m are octal numbers (0-7), then \nm will match the octal escape value nm.
\nml If n is an octal number (0-3), and both m and l are octal numbers (0-7), then the octal escape value nml is matched.
\un Match n, where n is a Unicode character represented by four hexadecimal numbers. For example, \u00A9 matches the copyright symbol (?).
/^\[ \t]*$/ "^\[ \t]*$" Matches a blank line.
/\d{2}-\d{5}/ "\d{2}-\d{5}" Verify whether an ID number consists of a 2-digit word, a hyphen and a 5-digit number.
/<(.*)>.*<\/\1>/ "<(.*)>.*<\/\1>" Matches an HTML tag.
The following table is a complete list of metacharacters and their behavior in the context of regular expressions:
Character Description
\ Mark the next character as a special character, or an primitive character, or a backward reference, or an octal escape character. For example, 'n' match the character "n". '\n' matches a newline character. The sequence '\\' matched "\" and "\(" matched "(".
^ Matches the start position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after '\n' or '\r'.
$ Match the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches the previous position of '\n' or '\r'.
* Match the previous subexpression zero or multiple times. For example, zo* can match "z" and "zoo". * Equivalent to {0,}.
+ Match the previous subexpression once or more times. For example, 'zo+' can match "zo" and "zoo", but cannot match "z". + equivalent to {1,}.
? Match the previous subexpression zero or once. For example, "do(es)?" can match "do" in "do" or "does". ? Equivalent to {0,1}.
{n} n is a non-negative integer. Match n times that are determined. For example, 'o{2}' cannot match 'o' in "Bob" , but can match 'o' in "food" .
{n,} n is a non-negative integer. Match at least n times. For example, 'o{2,}' cannot match 'o' in "Bob" , but can match all o in "fooooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.
{n,m} m and n are both non-negative integers, where n <= m. Match at least n and match at most m. Liu, "o{1,3}" will match the first three os in "foooooood". 'o{0,1}' is equivalent to 'o?'. Please note that there cannot be spaces between commas and two numbers.
? When the character is immediately followed by any other restriction character (*, +, ?, {n}, {n,}, {n,m}) , the matching pattern is non-greedy. The non-greedy pattern matches as few strings as possible, while the default greedy pattern matches as many strings as possible. For example, for the string "oooo", 'o+?' will match a single "o", and 'o+' will match all 'o'.
. Match any single character except "\n". To match anything including '\n'
For any character, please use the pattern like '[.\n]'.
(pattern) Match pattern and get this match. The obtained matches can be obtained from the generated Matches collection, using the SubMatches collection in VBScript, and the $0…$9 attribute in Visual Basic Scripting Edition. To match parentheses characters, use '\(' or '\)'.
(?:pattern) Match pattern but does not get the matching result, which means that this is a non-get match and is not stored for future use. This is useful when using the "or" character (|) to combine various parts of a pattern. For example, 'industr(?:y|ies) is an expression that is simpler than 'industry|industries'.
(?=pattern) Forward pre-check, match the search string at the beginning of any string matching pattern . This is a non-get match, that is, the match does not need to be retrieved for later use. For example, 'Windows (?=95|98|NT|2000)' can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3 .1".
Pre-checking does not consume characters, that is, after a match occurs, the next match's search begins immediately after the last match, rather than after the characters containing the pre-checking.
(?!pattern) Negative pre-check, match the search string at the beginning of any string that does not match Negative lookahead matches the search string at any point where a string not matching pattern . This is a non-get match, that is, the match does not need to be retrieved for later use. For example, 'Windows (?!95|98|NT|2000)' can match "Windows" in "Windows 3.1", but cannot match "Windows" in "Windows 2000". Pre-checking does not consume characters, that is, after a match occurs, the next match search begins immediately after the last match, rather than starting after the characters containing the pre-checking.
x|y match x or y. For example, 'z|food' can match "z" or "food". '(z|f) ood' matches "zood" or "food".
[xyz] Character collection. Match any character contained. For example, '[abc]' can match 'a' in "plain" .
[^xyz] The set of negative values characters. Match any characters not included. For example, '[^abc]' can match 'p' in "plain" .
[a-z] Character range. Match any character in the specified range. For example, '[a-z]' can match any lowercase alphabetical characters in the range 'a' to 'z'.
[^a-z] Negative value character range. Match any arbitrary characters that are not within the specified range. For example, '[^a-z]' can match any arbitrary character that is not in the range of 'a' to 'z'.
\b Match a word boundary, which means the position of the word and space. For example, 'er\b' can match 'er' in "never" , but cannot match 'er' in "verb".
\B Match non-word boundaries. 'er\B' can match 'er' in "verb" , but cannot match 'er' in "never".
\cx Matches the control characters specified by x. For example, \cM matches a Control-M or carriage return. The value of x must be one of A-Z or a-z. Otherwise, treat c as an original 'c' character.
\d Match a numeric character. Equivalent to [0-9].
\D Match a non-numeric character. Equivalent to [^0-9].
\f Match a page break. Equivalent to \x0c and \cL.
\n Match a newline character. Equivalent to \x0a and \cJ.
\r Match a carriage return character. Equivalent to \x0d and \cM.
\s Match any whitespace characters, including spaces, tabs, page breaks, etc. Equivalent to [ \f\n\r\t\v].
\S Match any non-whitespace characters. Equivalent to [^ \f\n\r\t\v].
\t Match a tab character. Equivalent to \x09 and \cI.
\v Match a vertical tab character. Equivalent to \x0b and \cK.
\w Match any word character that includes an underscore. Equivalent to '[A-Za-z0-9_]'.
\W Match any non-word character. Equivalent to '[^A-Za-z0-9_]'.
\xn Match n, where n is a hexadecimal escape value. The hexadecimal escape value must be the length of two numbers that are determined. For example, '\x41' matched "A". '\x041' is equivalent to '\x04' & "1". ASCII encoding can be used in regular expressions. .
\num Match num, where num is a positive integer. Reference to the obtained match. For example, '(.)\1' matches two consecutive identical characters.
\n Identifies an octal escape value or a backward reference. If \n before at least n retrieved subexpressions, then n is a backward reference. Otherwise, if n is an octal number (0-7), then n is an octal escape value.
\nm Identifies an octal escape value or a backward reference. If there are at least is preceded by at least nm retrieval subexpressions before, then nm is a backward reference. If there are at least n retrieves before \nm , then n is a hesitation text
Backward reference of word m. If the previous conditions are not satisfied, if both n and m are octal numbers (0-7), then \nm will match the octal escape value nm.
\nml If n is an octal number (0-3), and both m and l are octal numbers (0-7), then the octal escape value nml is matched.
\un Match n, where n is a Unicode character represented by four hexadecimal numbers. For example, \u00A9 matches the copyright symbol (?).