Advertisement

Literal Characters

Match exact characters.

abc     # Matches "abc"
123     # Matches "123"
hello   # Matches "hello"

Metacharacters

Special characters with meaning.

.       # Any character except newline
\d      # Any digit [0-9]
\D      # Any non-digit
\w      # Word character [a-zA-Z0-9_]
\W      # Non-word character
\s      # Whitespace character
\S      # Non-whitespace character

Escaping Special Characters

Use backslash to match literals.

\.      # Matches literal period
\*      # Matches literal asterisk
\?      # Matches literal question mark
\\      # Matches literal backslash
\[      # Matches literal bracket
Advertisement

Alternation

Match one pattern or another.

cat|dog           # Matches "cat" or "dog"
(red|blue|green)  # Matches any color
yes|no            # Matches "yes" or "no"

Dot Metacharacter

Match any character.

a.c     # Matches "abc", "a1c", "a@c"
...     # Matches any 3 characters
h.t     # Matches "hat", "hot", "h9t"

Case Sensitivity

Regex is case-sensitive by default.

cat     # Matches "cat" but not "Cat"
# Use flag for case-insensitive: /cat/i

Literal Strings

Match exact sequences.

hello world   # Matches "hello world"
test123       # Matches "test123"

Common Examples

Practical regex patterns.

\d\d\d-\d\d\d\d    # Phone: 123-4567
\w+@\w+\.com       # Simple email
https?://.*        # URL starting with http/https
Last updated: January 2026
Advertisement