Regex Cheat Sheet & Demo
Learn and test regular expressions with an interactive demo
Class | Description | Example |
---|
[abc] | Matches any single character in the set | [aeiou] matches "a" in "cat" |
[^abc] | Matches any single character not in the set | [^0-9] matches "a" in "a1" |
[a-z] | Matches any character in the range | [a-z] matches "b" in "abc" |
[A-Z] | Matches any uppercase letter | [A-Z] matches "B" in "ABC" |
[0-9] | Matches any digit | [0-9] matches "5" in "a5b" |
[a-zA-Z0-9] | Matches any alphanumeric character | [a-zA-Z0-9] matches "x" in "x1" |
Quantifier | Description | Example |
---|
* | Matches 0 or more occurrences | a* matches "" or "aaa" |
+ | Matches 1 or more occurrences | a+ matches "a" or "aaa" |
? | Matches 0 or 1 occurrence | colou?r matches "color" or "colour" |
{n} | Matches exactly n occurrences | \d{3} matches "123" |
{n,} | Matches n or more occurrences | \d{2,} matches "1234" |
{n,m} | Matches between n and m occurrences | \d{2,4} matches "123" |
*? , +? , ?? | Non-greedy (lazy) matching | <.*?> matches "<p>" in "<p><span>" |
Group | Description | Example |
---|
(...) | Captures a group | (\d+) captures "123" in "abc123" |
(?:...) | Non-capturing group | (?:\d+) groups "123" without capturing |
(?<name>...) | Named capture group | (?<year>\d{4}) captures "2023" as "year" |
\1 , \2 | Backreference to captured group | (\w+)\s\1 matches "word word" |
| | Alternation (OR) | cat|dog matches "cat" or "dog" |
Assertion | Description | Example |
---|
(?=...) | Positive lookahead | \w+(?=\.) matches "file" in "file.txt" |
(?!...) | Negative lookahead | \w+(?!\.) matches "file" in "filename" |
(?<=...) | Positive lookbehind | (?<=\$)\d+ matches "100" in "$100" |
(?<!...) | Negative lookbehind | (?<!\$)\d+ matches "100" in "100px" |
Flag | Description | Example |
---|
g | Global: Matches all occurrences | /a/g matches all "a"s in "aba" |
i | Ignore case: Case-insensitive matching | /a/i matches "A" or "a" |
m | Multiline: ^ and $ match line start/end | /^a/m matches "a" at start of each line |
y | Sticky: Matches only at lastIndex | /a/y matches "a" at specific position |
u | Unicode: Enables full Unicode matching | /\p{L}/u matches any Unicode letter |
s | Dotall: . matches newlines | /a.b/s matches "a\nb" |
Pattern | Description | Example |
---|
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} | Matches an email address | Matches "user@example.com" |
https?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/[^\s]*)? | Matches a URL | Matches "https://example.com/path" |
\+?\d{1,3}[-.\s]?\d{3}[-.\s]?\d{4} | Matches a phone number | Matches "+1-555-123-4567" |
#?[0-9a-fA-F]{6} | Matches a hex color code | Matches "#FF0000" |
\d{4}-\d{2}-\d{2} | Matches a date (YYYY-MM-DD) | Matches "2023-12-31" |
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} | Matches an IPv4 address | Matches "192.168.1.1" |
- Use the demo above to test patterns from the cheat sheet
- Escape special characters with \ (e.g., \. for literal dot)
- Enable flags like Global (g) or Ignore Case (i) in the demo for different behaviors
- Test with varied inputs to ensure patterns are robust
- Use named capture groups (?...) for clarity