Regular Expressions - Summary of Usage
The following lists a summary of usage for some common regular expressions:
Matching basic characters
-
Using literal character matching: literal characters (such as letters, digits, symbols) represent themselves in a regular expression, for example to match "runoob":
/runoob/. Try it »
-
Using metacharacters
.Matching any character:.matches any single character, for example to match "cat" or "cbt":/c.t/. Try it »
Matching character sets
-
Using character set matching: use square brackets
[]to denote a character set, matching any character in the set, for example to match "cat", "bat", or "hat":/[cbh]at/.Try it »
-
Using hyphens
-Denoting a character range: use a hyphen within a character set-to match a range of characters, for example to match lowercase letters from "a" to "z":/[a-z]/.Try it »
Matching repetition and quantity
-
Use
*Matching zero or more:*matches the preceding pattern zero or more times, for example to match "caat", "ct", or "cat":/ca*t/.Try it »
-
Use
+Matching one or more:+matches the preceding pattern one or more times, for example to match "cat", "caat", "caaat", etc.:/ca+t/.Try it »
-
Use
?Matching zero or one:?matches the preceding pattern zero or once, for example to match "ct" or "cat":/ca?t/.Try it »
-
Using curly braces
{n}Matching a fixed quantity: use curly braces{n}matches the preceding pattern exactly n times, for example to match "caat", "caat123", or "caataaa":/ca{2}t/.Try it »
Using special characters and escaping
-
Escaping special characters: use a backslash
\to escape special characters, for example to match "2+2=4":/2\+2=4/.Try it »
-
Matching boundaries and positions: use special characters
^and$to denote the beginning and end of a line respectively, for example to match a string starting with "hello":/^hello/.Try it »
Using capture groups
-
Using parentheses
()Creating a capture group: use parentheses()to combine part of a regular expression pattern into a capture group, allowing you to extract or group matched content, for example to extract the year from a date:/(\d{4})-\d{2}-\d{2}/.Try it »
Using character escaping
-
Using a backslash
\Escaping special characters: for special characters in regular expressions, such as*,+,?, etc., use a backslash\to escape them, for example to match a string containing a literal asterisk:/\*/.Try it »
Using character classes
-
Using predefined character classes: regular expressions provide some predefined character classes, for example
\dmatches any digit character,\wmatches any letter, digit, or underscore character,\smatches any whitespace character, etc.Try it »
Using logical operators
-
Use
|Performing a logical OR operation:|is used to represent a logical OR operation and can match any one of multiple patterns, for example to match "cat" or "dog":/(cat|dog)/.Try it »
Using backreferences
-
Use
\nPerforming a backreference: in a regular expression,\ncan be used to reference the matched content of a previously captured group, for example to match repeated words:/\b(\w+)\b\s+\1\b/.Try it »
Using modifiers
-
Using modifiers to change matching behavior: regular expressions support some modifiers, such as
ifor case-insensitive matching,gfor global matching,mfor multiline matching, etc.