Regular Expression - Metacharacters
Metacharacters in regular expressions are characters with special meanings; they do not represent literal meaning but are used to control the matching pattern.
Basic Metacharacters
. (Dot)
-
Matches any single character except a newline (
\n).Example:
a.bMatches "aab", "a1b", "a b", etc.
^ (Caret)
-
Matches the start position of a string
-
Example:
^abcMatches strings that start with "abc"
$ (Dollar Sign)
-
Matches the end position of a string
-
Example:
xyz$Matches strings that end with "xyz"
\ (Backslash)
-
Escape character that removes the special meaning of the following character
-
Example:
\.Matches a literal dot rather than any character
Character Class Metacharacters
[] (Square Brackets)
-
Defines a character set, matching any one character within it
-
Example:
[aeiou]Matches any vowel letter
[^] (Negated Character Class)
-
Matches any character not in the square brackets
-
Example:
[^0-9]Matches any non-digit character
- (Hyphen)
-
Represents a range within a character class
-
Example:
[a-z]Matches any lowercase letter
Quantifier Metacharacters
* (Asterisk)
-
Matches the preceding subexpression zero or more times
-
Example:
ab*cMatches "ac", "abc", "abbc", etc.
+ (Plus Sign)
-
Matches the preceding subexpression one or more times
-
Example:
ab+cMatches "abc", "abbc" but not "ac"
? (Question Mark)
-
Matches the preceding subexpression zero or once
-
Example:
colou?rMatches "color" and "colour"
{n} (Curly Braces)
-
Matches exactly n times
-
Example:
a{3}Matches "aaa"
{n,}
-
Matches at least n times
-
Example:
a{2,}Matches "aa", "aaa", etc.
{n,m}
-
Matches between n and m times
-
Example:
a{2,4}Matches "aa", "aaa", "aaaa"
Grouping and Alternation Metacharacters
() (Parentheses)
-
Defines a subexpression or capturing group
-
Example:
(ab)+Matches "ab", "abab", etc.
| (Vertical Bar)
-
Represents an "or" relationship
-
Example:
cat|dogMatches "cat" or "dog"
Special Character Class Metacharacters
\d
-
Matches any digit, equivalent to
[0-9]
\D
-
Matches any non-digit, equivalent to
[^0-9]
\w
-
Matches any word character (letter, digit, underscore), equivalent to
[a-zA-Z0-9_]
\W
-
Matches any non-word character, equivalent to
[^a-zA-Z0-9_]
\s
-
Matches any whitespace character (space, tab, newline, etc.)
\S
-
Matches any non-whitespace character
Boundary Matching Metacharacters
\b
-
Matches a word boundary
-
Example:
\bcat\bMatches "cat" but not "category"
\B
-
Matches a non-word boundary
-
Example:
\Bcat\BMatches "cat" in "scattered" but not standalone "cat"
Other Metacharacters
\n
-
Matches a newline
\t
-
Matches a tab
\r
-
Matches a carriage return
\f
-
Matches a form feed
\v
-
Matches a vertical tab
Greedy and Non-greedy Quantifiers
By default, quantifiers (*, +, ?, {}) are greedy and match as many characters as possible. Adding?after a quantifier switches it to non-greedy (lazy) mode:
-
*?: zero or more times, but as few as possible -
+?: one or more times, but as few as possible -
??: zero or one time, but as few as possible -
{n,m}?: between n and m times, but as few as possible
Example:<.*?> When matching HTML tags, it does not match across tags
Positive and Negative Lookahead
(?=...) (Positive Lookahead)
-
Matches a position followed by a specific pattern
-
Example:
Windows(?=95|98)Matches "Windows" followed by 95 or 98
(?!...) (Negative Lookahead)
-
Matches a position not followed by a specific pattern
-
Example:
Windows(?!95|98)Matches "Windows" not followed by 95 or 98
(?<=...) (Positive Lookbehind)
-
Matches a position preceded by a specific pattern
-
Example:
(?<=95|98)WindowsMatches "Windows" preceded by 95 or 98
(? (反向否定预查)
-
Match a position that is not preceded by a specific pattern
-
Example:
(? 匹配前面不是95或98的"Windows"
Example
Next we analyze a regular expression that matches email addresses, as shown below:
Example
var
str
=
"
abcd test@runoob.com 1234
"
;
var
patt1
= /\
b
[
\
w
.%+-
]
+@
[
\
w
.-
]
+.
[
a
zA
Z
]
{
2
,
6
}
\
b
/
g
;
document
.
write
(
str
.
match
(
patt1
)
)
;
The text marked below is the matched expression:
test@runoob.com

Try it »

The following table contains the complete list of metacharacters and their behavior in a regular expression context:
| Character | Description |
|---|---|
| \ |
Marks the next character as a special character, a literal character, a backreference, or an octal escape. For example, 'n' matches the character "n". '\n' matches a newline. The sequence '\' matches "" while "(" matches "(. |
| ^ |
Match the start position of the input string. If the RegExp object's Multiline property is set, ^ also matches the position after '\n' or '\r'. |
| $ |
Match the end position of the input string. If the RegExp object's Multiline property is set, $ also matches the position before '\n' or '\r'. |
| * |
Match the preceding subexpression zero or more times. For example, zo* can match "z" and "zoo". * is equivalent to {0,}. |
| + |
Match the preceding subexpression one or more times. For example, 'zo+' can match "zo" and "zoo", but cannot match "z". + is equivalent to {1,}. |
| ? |
Match the preceding subexpression zero or one time. For example, "do(es)?" can match "do" or "does". ? is equivalent to {0,1}. |
| {n} |
n is a non-negative integer. Match exactly n times. For example, 'o{2}' cannot match the 'o' in "Bob", but can match the two o's in "food". |
| {n,} |
n is a non-negative integer. Match at least n times. For example, 'o{2,}' cannot match the 'o' in "Bob", but can match all the o's in "foooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'. |
| {n,m} |
Both m and n are non-negative integers, where n <= m. Match at least n times and at most m times. For example, "o{1,3}" will match the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o?'. Note that there must be no space between the comma and the two numbers. |
| ? |
When this character immediately follows any other quantifier (*, +, ?, {n}, {n,}, {n,m}), the matching mode is non-greedy. The non-greedy mode matches as few of the searched string as possible, while the default greedy mode matches as many as possible. For example, for the string "oooo", 'o+?' matches a single "o", while 'o+' matches all the 'o's. |
| . |
Match any single character except a newline (\n, \r). To match any character including '\n', use something like "(.|\n)" pattern." |
| (pattern) |
Match pattern and capture this match. The captured match can be obtained from the resulting Matches collection; in VBScript use the SubMatches collection, and in JScript use the $0...$9 properties. To match parentheses characters, use '(' or ')'. |
| (?:pattern) |
Match pattern but do not capture the match result, that is, this is a non-capturing match, not stored for later use. This is useful when using the "or" character (|) to combine the parts of a pattern. For example, 'industr(?:y|ies) is a more concise expression than 'industry|industries'. |
| (?=pattern) |
Positive lookahead (look ahead positive assert), matches the search string at the start of any string that matches pattern. This is a non-capturing match, meaning the match does not need to be captured for later use. For example, "Windows(?=95|98|NT|2000)" can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3.1". The lookahead does not consume characters, meaning that after a match occurs, the next match search starts immediately after the last match, not after the characters included in the lookahead. |
| (?!pattern) |
Positive negative lookahead (negative assert), matches the search string at the start of any string that does not match pattern. This is a non-capturing match, meaning the match does not need to be captured for later use. For example, "Windows(?!95|98|NT|2000)" can match "Windows" in "Windows3.1", but cannot match "Windows" in "Windows2000". The lookahead does not consume characters, meaning that after a match occurs, the next match search starts immediately after the last match, not after the characters included in the lookahead. |
| (?<=pattern) | Reverse (look behind) positive lookahead, similar to positive lookahead except the direction is reversed. For example, "(?<=95\|98\|NT\|2000)Windows" can match"2000Windows" in the "Windows", but cannot match "3.1Windows" in the "Windows". |
| (? | Reverse negative lookahead, similar to negative lookahead except the direction is reversed. For example"(?" can match" |
| x|y |
Match x or y. For example, 'z|food' can match "z" or "food". '(z|f)ood' matches "zood" or "food". |
| [xyz] |
Character set. Match any one of the included characters. For example, '[abc]' can match the 'a' in "plain". |
| [^xyz] |
Negative character set. Match any character not included. For example, '[^abc]' can match the 'p', 'l', 'i', 'n' in "plain". |
| [a-z] |
Character range. Match any character within the specified range. For example, '[a-z]' can match any lowercase letter character from 'a' to 'z'. |
| [^a-z] |
Negative character range. Match any character not within the specified range. For example, '[^a-z]' can match any character not in the range 'a' to 'z'. |
| \b |
Match a word boundary, that is, the position between a word and a space. For example, 'er\b' matches the 'er' in "never" but not the 'er' in "verb". |
| \B |
Match a non-word boundary. 'er\B' matches the 'er' in "verb" but not the 'er' in "never". |
| \cx |
Match the control character 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 a literal 'c' character. |
| \d |
Match a digit character. Equivalent to [0-9]. |
| \D |
Match a non-digit character. Equivalent to [^0-9]. |
| \f |
Match a form feed. Equivalent to \x0c and \cL. |
| \n |
Match a newline. Equivalent to \x0a and \cJ. |
| \r |
Match a carriage return. Equivalent to \x0d and \cM. |
| \s |
Match any whitespace character, including spaces, tabs and form feeds. Equivalent to [ \f\n\r\t\v]. |
| \S |
Match any non-whitespace character. Equivalent to [^ \f\n\r\t\v]. |
| \t |
Match a tab character. Equivalent to \x09 and \cI. |
| \v |
Match a vertical tab. Equivalent to \x0b and \cK. |
| \w |
Match letters, digits, and underscores. Equivalent to '[A-Za-z0-9_]'. |
| \W |
Match non-letters, digits, and underscores. Equivalent to '[^A-Za-z0-9_]'. |
| \xn |
Match n, where n is a hexadecimal escape value. The hexadecimal escape value must be exactly two digits long. For example, '\x41' matches "A". '\x041' is equivalent to '\x04' & "1". ASCII encoding can be used in regular expressions. |
| \num |
Match num, where num is a positive integer. A reference to the obtained match. For example, '(.)\1' matches two consecutive identical characters. |
| \n |
Identifies an octal escape value or a backreference. If there are at least n captured subexpressions before \n, then n is a backreference. Otherwise, if n is an octal digit (0-7), then n is an octal escape value. |
| \nm |
Identifies an octal escape value or a backreference. If there are at least nm captured subexpressions before \nm, then nm is a backreference. If there are at least n captures before \nm, then n is a backreference followed by the literal m. If none of the above conditions are met, and both n and m are octal digits (0-7), then \nm matches the octal escape value nm. |
| \nml |
If n is an octal digit (0-3), and m and l are both octal digits (0-7), then match the octal escape value nml. |
| \un |
Match n, where n is a Unicode character represented by four hexadecimal digits. For example, \u00A9 matches the copyright symbol (?). |