Regex Special Characters: The Complete Reference
Regular expressions (regex) are powerful tools for pattern matching and text manipulation, and understanding regex special characters is crucial for mastering them. This guide will explain each metacharacter, how to escape them, and which characters have special meanings inside character classes.
Core Metacharacters and Their Functions
Below are the core metacharacters in regex, along with their functions and examples:
- . – Matches any single character except newline characters.
a.cmatches “abc”, “a1c”, etc. - ^ – Asserts the start of a line or negates a character class if used inside
[].^Hellomatches “Hello” at the start of a line. - $ – Asserts the end of a line.
world$matches “world” at the end of a line. - * – Matches zero or more of the preceding element.
ab*cmatches “ac”, “abc”, “abbbc”, etc. - + – Matches one or more of the preceding element.
ab+cmatches “abc”, “abbbc”, etc., but not “ac”. - ? – Matches zero or one of the preceding element or makes the preceding quantifier lazy.
ab?cmatches “ac” and “abc”. - () – Groups a sequence of characters or captures a sub-expression.
(ab)+matches “ab”, “abab”, etc. - [] – Defines a regex character class.
[a-z]+matches one or more lowercase letters. - {} – Specifies a quantifier count.
a{3}matches “aaa”. - | – Acts as an alternation operator.
cat|dogmatches “cat” or “dog”. - \ – Escapes the next character or introduces a special sequence.
\.matches a literal dot.
Escaping a Metacharacter
To match a metacharacter literally, you need to escape it with a backslash (\). For example, to match a literal dot, use \.. This applies to all metacharacters listed above.
Which Characters Are Special Inside []
Inside a regex character class, only the following characters have special meanings:
- ^ – If it is the first character, it negates the class.
[^a-z]matches any character that is not a lowercase letter. - – – Indicates a range of characters if between two characters.
[a-z]matches any lowercase letter. - ] – Marks the end of the character class.
To include a literal], place it as the first character or escape it. - \ – Escapes the next character.
To include a literal\, escape it with another\.
All other metacharacters like ., *, +, and ? are treated as literal characters inside [].
Reference Table
| Character | Meaning | Example |
|---|---|---|
| . | Any character | a.c |
| ^ | Start of line / Negation in [] | ^Start / [^a-z] |
| $ | End of line | End$ |
| * | Zero or more | ab*c |
| + | One or more | ab+c |
| ? | Zero or one / Lazy modifier | ab?c |
| () | Grouping | (ab)+ |
| [] | Character class | [a-z]+ |
| {} | Quantifier count | a{3} |
| | | Alternation | cat|dog |
| \ | Escape | \. |
Anchors and boundaries
Regular expressions often use special characters called anchors and boundaries to assert the position of a match within a string. These do not match actual characters but rather specific positions.
- ^ asserts the start of a line. For example,
^Hellomatches “Hello” only at the beginning of a line. - $ asserts the end of a line. For example,
world$matches “world” only at the end of a line. - \b asserts a word boundary. For example,
\bcat\bmatches “cat” as a whole word, not as part of “caterpillar”. - \B asserts a non-word boundary. For example,
\Bcat\Bmatches “cat” only if it is surrounded by word characters, like in “concatenate”.
Shorthand classes and common mistakes
Regular expressions provide several shorthand character classes for common types of characters. These are widely used but can be a source of confusion if not understood properly.
- \d matches any digit (equivalent to
[0-9]). - \w matches any word character (equivalent to
[A-Za-z0-9_]). - \s matches any whitespace character (including spaces, tabs, and line breaks).
- \D, \W, and \S are the negations of the above:
- \D matches any non-digit character.
- \W matches any non-word character.
- \S matches any non-whitespace character.
One of the most common mistakes when using these shorthand classes is forgetting to escape a literal dot (.) or a literal backslash (\). For example, to match a literal dot, you need to use \.. Failing to do so will result in the dot being interpreted as a wildcard that matches any character except a newline.
For instance, consider the regular expression \d\.\d. This matches a digit followed by a literal dot and another digit, such as “3.5”. If you mistakenly write \d.\d, it would match a digit followed by any character and another digit, like “3a5” or “3#5”.
Using QuikConsole’s Regex Tools
For testing and debugging regex patterns, you can use the regex tester tool available in QuikConsole. It runs entirely in your browser, ensuring your data never leaves your machine. This tool is perfect for experimenting with the metacharacters and patterns discussed in this guide.
Escaping user input safely
When constructing a regular expression from user input or any untrusted source, it is crucial to escape every metacharacter. This prevents the input from altering the intended structure of your regex, which could lead to unexpected behavior or security vulnerabilities such as regex injection.
For example, if you are searching for a literal string provided by a user, you should escape all regex metacharacters in that string before using it in a regex pattern. Here is a small conceptual example in JavaScript:
function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
const userInput = "price is $10 (ten dollars)";
const escapedInput = escapeRegex(userInput);
const regex = new RegExp(escapedInput);
console.log(regex); // Outputs: /price is \$10 \(ten dollars\)/
In this example, the escapeRegex function ensures that any metacharacter in the user input is treated as a literal character by prefixing it with a backslash.
Quick cheat sheet
- ^ – Start of a line
- $ – End of a line
- * – Zero or more occurrences
- + – One or more occurrences
- ? – Zero or one occurrence
- {} – Specify exact number of occurrences, e.g.,
{3}for exactly three - () – Capturing group
- [] – Character class, e.g.,
[A-Z]for any uppercase letter - \ – Escape character, used to escape special characters or denote special sequences
- | – Alternation, e.g.,
cat|dogfor “cat” or “dog”
Frequently asked questions
What are the core metacharacters in regex?
The core metacharacters are: . ^ $ * + ? ( ) [ ] { } | and the backslash (\).
How do I escape a metacharacter in regex?
Precede the metacharacter with a backslash (\). For example, \. matches a literal dot.
What does the asterisk (*) mean in regex?
The asterisk (*) matches zero or more of the preceding element.
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers match as much as possible, while lazy quantifiers match as little as possible. Learn more at greedy vs lazy quantifiers.
How do I use a character class in regex?
Enclose the characters in square brackets []. For example, [a-z] matches any lowercase letter. Learn more at regex character class.