Stop googling syntax. The ultimate reference guide for Regular Expression character classes, quantifiers, assertions, and boundary anchors.
Anchors
^Start of string (or line with m flag)Copy$End of string (or line with m flag)Copy\bWord boundaryCopy\BNot a word boundaryCopy\AStart of string (not all engines)Copy\ZEnd of string (not all engines)CopyCharacter Classes
.Any character except newline (use s flag for newlines)Copy\dAny digit [0-9]Copy\DAny non-digitCopy\wWord character [a-zA-Z0-9_]Copy\WNon-word characterCopy\sWhitespace (space, tab, newline)Copy\SNon-whitespaceCopy[abc]Character set: a, b, or cCopy[^abc]Negated set: not a, b, or cCopy[a-z]Range: any lowercase letterCopyQuantifiers
*Zero or more (greedy)Copy+One or more (greedy)Copy?Zero or one (greedy)Copy{n}Exactly n timesCopy{n,}n or more timesCopy{n,m}Between n and m timesCopy*?Zero or more (lazy)Copy+?One or more (lazy)Copy??Zero or one (lazy)CopyGroups & Lookaround
(abc)Capture group — accessible as $1, $2…Copy(?:abc)Non-capturing groupCopy(?<name>abc)Named capture groupCopy(?=abc)Positive lookahead: followed by abcCopy(?!abc)Negative lookahead: not followed by abcCopy(?<=abc)Positive lookbehind: preceded by abcCopy(?<!abc)Negative lookbehind: not preceded by abcCopy(a|b)Alternation: a or bCopy\1Backreference to capture group 1CopyFlags
gGlobal — find all matches, not just the firstCopyiCase-insensitive matchingCopymMultiline — ^ and $ match line starts/endsCopysDot-all — . matches newlines tooCopyuUnicode — enables full Unicode matchingCopyySticky — match only from lastIndex positionCopyEscape Sequences
\nNewlineCopy\tTabCopy\rCarriage returnCopy\0Null characterCopy\uXXXXUnicode code point (e.g. \u0041 = A)Copy\.Literal dot (escape metacharacters with \)CopyUnderstand the core building blocks required to parse massive, chaotic text blocks efficiently.
Never write `[0-9]` or `[A-Za-z]` again. The cheatsheet maps out the critical shorthand tokens (like `\d` and `\w`) required to write dense, highly optimized validation patterns.
A massive pitfall for junior developers. Learn exactly how adding a `?` quantifier forces an aggressive pattern to stop searching at the first valid result instead of consuming the entire document.
Master advanced Lookahead `(?=)` and Lookbehind `(?<=)` architecture. These commands allow the engine to check surrounding characters for validation without actually capturing them in the final output.
Regular Expressions are not a programming language; they are a mathematical matching engine. Writing a Regex pattern is essentially giving the engine a hyper-specific set of instructions on how to traverse a chaotic string of text to locate incredibly precise data points.
If you want to search a document for the price $50.00, you cannot just type $50.00 into the pattern. It will fail.
This is because symbols like $, ., and ? are "Metacharacters". They are built-in commands. The $ tells the engine "End of Line". The . tells the engine "Any character in the universe".
If you need to find a literal dollar sign, you must "escape" its mathematical power by placing a backslash before it. A regex cheatsheet is critical because it reminds developers that the correct pattern is actually \$50\.00.
How do you find the word "cat" without matching the word "catalog"?
If your pattern is simply cat, the engine is blind to human language constraints. It scans the word "catalog", sees the letters c-a-t, flags a successful match, and ruins your database extraction.
You must use the Word Boundary anchor: \b. If you write \bcat\b, you are commanding the engine to only trigger a match if the word is surrounded by non-word characters (like spaces or punctuation). It perfectly isolates "cat" and ignores "catalog".
Parentheses () are incredibly powerful. If you write (http|https), you are creating a Capture Group. The engine will match the URL, but it will also mathematically slice out the "https" and save it to memory so you can use it later in your code.
?: inside the brackets: (?:http|https). This tells the engine "group these words together for matching, but do not waste memory saving them to an array".