Regex (Regular Expressions) Cheat Sheet - WittyWriter

Regex (Regular Expressions) Cheat Sheet

📘 Key Concepts and Definitions

🧮 Pattern Structure & Flags

In many languages (like JavaScript), regex patterns are defined between slashes, followed by flags.

/pattern/flags

Common Flags

Example: /hello/gi would find all occurrences of "hello", "Hello", "HELLO", etc., in a string.

🛠️ Core Syntax & Metacharacters

Anchors & Boundaries

CharacterDescriptionExample
^Asserts position at the start of the string (or line in multiline mode).^A matches "A" in "Apple"
$Asserts position at the end of the string (or line in multiline mode).e$ matches "e" in "Apple"
\bWord boundary. Matches a position between a word and a non-word character.\bcat\b matches "cat" in "The cat sat." but not in "caterpillar".

Character Classes

CharacterDescription
.Matches any single character except newline.
\dMatches any digit (0-9). Equivalent to [0-9].
\DMatches any non-digit.
\wMatches any word character (alphanumeric + underscore). Equivalent to [A-Za-z0-9_].
\WMatches any non-word character.
\sMatches any whitespace character (space, tab, newline).
\SMatches any non-whitespace character.
[...]Character set. Matches any one of the characters inside the brackets (e.g., [aeiou]).
[^...]Negated character set. Matches any character not inside the brackets.

Quantifiers

QuantifierDescription
*Matches the preceding item 0 or more times.
+Matches the preceding item 1 or more times.
?Matches the preceding item 0 or 1 time. (Makes it optional).
{n}Matches the preceding item exactly n times.
{n,}Matches the preceding item n or more times.
{n,m}Matches the preceding item between n and m times.

Groups & Alternation

SyntaxDescription
(...)Grouping. Groups multiple tokens together and creates a capture group.
(?:...)Non-capturing group. Groups tokens but does not create a capture group.
|Alternation (OR). Matches either the expression before or after the pipe.

🧭 Workflow for Building a Regex

  1. Define the Target: Be very clear about what you want to match and what you want to exclude. Find sample strings.
  2. Start with Literals: Begin by matching the parts of the string that are always the same.
  3. Generalize with Classes: Replace specific characters with character classes (e.g., replace 5 with \d).
  4. Add Quantifiers: Specify how many times characters or groups should repeat (e.g., use \d+ for one or more digits).
  5. Set Boundaries: Use anchors (^, $) and word boundaries (\b) to prevent partial matches.
  6. Test and Refine: Use an online tool like Regex101 to test your pattern against various inputs and refine it.

⌨️ Productivity Tips

📊 Quick Reference Table

This table summarizes the most common tokens.

TokenMeaningTokenMeaning
.Any character\dDigit
\wWord character\sWhitespace
[...]Character set^Start of string
$End of string*0 or more
+1 or more?0 or 1
(...)Capture group|OR

🧪 Common Use Case Examples

Validate a Simple Email Address

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

Match a US Phone Number

/^\(?(\d{3})\)?[-\s]?(\d{3})[-\s]?(\d{4})$/

This pattern matches formats like (123) 456-7890, 123-456-7890, and 123 456 7890.

Extract a URL from Text

/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gi

A more complex example that finds http and https URLs.

🧹 Troubleshooting Common Issues

📚 References and Further Reading

🍪 We use cookies to improve your experience. Learn more