regex cheatsheetregular expressionsregex patternsstring matchingdeveloper reference

Regex Cheatsheet: Every Pattern You Need for Regular Expressions

A practical reference for regular expression syntax. Covers character classes, quantifiers, anchors, groups, lookaheads, and common patterns with examples.

7 min read

Related Tool

Regex Cheatsheet

Open tool

Regular expressions are one of the most powerful and reused tools in a developer's toolkit. A single regex pattern can replace dozens of lines of manual string parsing code. The challenge is that regex syntax is compact to the point of appearing cryptic until you learn the building blocks. This cheatsheet walks through every core concept with practical examples.

Characters and Literals

A regex pattern matches against text. Most characters match themselves: the pattern "hello" matches the literal string "hello" wherever it appears.

Special characters (called metacharacters) have special meaning: dot, caret, dollar, asterisk, plus, question mark, open and close parentheses, open and close square brackets, open and close curly braces, pipe, and backslash. To match a literal metacharacter, escape it with a backslash.

The Dot

A dot matches any single character except a newline (in most modes). The pattern "c.t" matches "cat", "cut", "cot", and even "c1t". Use dot-star (any number of any characters) cautiously because it is greedy and can match more than intended.

Character Classes

Square brackets define a character class that matches any one character from the set. [aeiou] matches any vowel. [a-z] matches any lowercase letter. [0-9] matches any digit. [a-zA-Z0-9_] matches word characters.

A caret at the start of a character class negates it: [^aeiou] matches any character that is NOT a vowel.

Shorthand Character Classes

Backslash sequences are shorthand for common classes.

d matches any digit (equivalent to [0-9]).

D matches any non-digit.

w matches any word character (letters, digits, underscore).

W matches any non-word character.

s matches any whitespace (space, tab, newline, carriage return).

S matches any non-whitespace.

Quantifiers

Quantifiers specify how many times the preceding element must match.

Asterisk: zero or more. "a*" matches zero or more a's.

Plus: one or more. "a+" matches one or more a's.

Question mark: zero or one (optional). "colou?r" matches both "color" and "colour".

Curly braces exact count: "a{3}" matches exactly three a's.

Curly braces range: "a{2,4}" matches two, three, or four a's.

Curly braces minimum: "a{2,}" matches two or more a's.

Quantifiers are greedy by default: they match as much as possible. Adding a question mark after a quantifier makes it lazy (matches as little as possible): ".*?" finds the shortest possible match.

Anchors

Anchors match positions rather than characters.

Caret outside a character class matches the start of the string (or start of a line in multiline mode).

Dollar sign matches the end of the string (or end of a line in multiline mode).

 matches a word boundary: the position between a word character and a non-word character.

B matches a non-word boundary.

The pattern "word" matches "word" as a whole word but not "password" or "swordfish".

Groups and Alternation

Parentheses create a capturing group that can be referenced. "(cat|dog)" matches either "cat" or "dog". The pipe character is alternation: this or that.

Non-capturing groups use (?: prefix: "(?:cat|dog)" groups without capturing.

Backreferences refer to a captured group by number. A backslash followed by 1 (written as a two-character sequence in your code) refers to the first captured group, allowing you to match repeated words like "the the" by requiring the same word to appear twice in a row.

Lookaheads and Lookbehinds

Lookahead assertions match text that is followed by a pattern without consuming it.

Positive lookahead: "foo(?=bar)" matches "foo" only when followed by "bar".

Negative lookahead: "foo(?!bar)" matches "foo" only when NOT followed by "bar".

Positive lookbehind: "(?<=foo)bar" matches "bar" only when preceded by "foo".

Negative lookbehind: "(?<!foo)bar" matches "bar" only when NOT preceded by "foo".

Support for lookbehind varies by language. JavaScript added it in ES2018.

Flags and Modifiers

Regex engines support flags that modify behavior.

i: case-insensitive matching.

g: global flag (find all matches, not just the first).

m: multiline (caret and dollar match line starts and ends).

s: dotall mode (dot matches newlines too).

x: extended mode (ignores whitespace in pattern for readability).

Common Patterns

Email (simplified): [w.+-]+@[w-]+.[a-zA-Z]{2,}

URL (simplified): https?://S+

Phone (US): (?d{3})?[-.s]d{3}[-.s]d{4}

IPv4: d{1,3}(.d{1,3}){3}

Using the DevHexLab Regex Cheatsheet

Open the reference at /tools/reference/regex-cheatsheet. Browse syntax categories, copy patterns, and jump to the regex tester to test them against your own input.