Missing something?

Looked cheats

A concise reference for regular expressions, covering common syntax, metacharacters, and usage examples to help you quickly craft and understand regex patterns.

Regex Basics & Metacharacters

Basic Matching

/pattern/ - Matches the literal string ‘pattern’.

Example:
/hello/ matches ‘hello’ in ‘hello world’.

. - Matches any single character except newline.

Example:
/.at/ matches ‘cat’, ‘hat’, ‘sat’, etc.

\ - Escapes a special character to treat it literally.

Example:
/\./ matches a literal dot (‘.’).

^ - Matches the beginning of the string.

Example:
/^hello/ matches ‘hello’ only if it’s at the start.

$ - Matches the end of the string.

Example:
/world$/ matches ‘world’ only if it’s at the end.

| - Acts as an ‘OR’ operator.

Example:
/cat|dog/ matches either ‘cat’ or ‘dog’.

Character Classes

[abc] - Matches any single character ‘a’, ‘b’, or ‘c’.

Example:
/[bc]at/ matches ‘bat’ or ‘cat’.

[^abc] - Matches any single character except ‘a’, ‘b’, or ‘c’.

Example:
/[^bc]at/ matches ‘hat’ but not ‘bat’ or ‘cat’.

[a-z] - Matches any lowercase letter from ‘a’ to ‘z’.

Example:
/[a-z]at/ matches ‘bat’, ‘cat’, ‘dat’, etc.

[A-Z] - Matches any uppercase letter from ‘A’ to ‘Z’.

Example:
/[A-Z]at/ matches ‘Bat’, ‘Cat’, ‘Dat’, etc.

[0-9] - Matches any digit from 0 to 9.

Example:
/[0-9]at/ matches ‘1at’, ‘2at’, ‘3at’, etc.

\w - Matches any word character (letters, numbers, and underscore).

Example:
/\w+/ matches ‘hello’, ‘world123’, ‘abc_def’.

Quantifiers & Grouping

Quantifiers

* - Matches the preceding character zero or more times.

Example:
/a*/ matches ‘’, ‘a’, ‘aa’, ‘aaa’, etc.

+ - Matches the preceding character one or more times.

Example:
/a+/ matches ‘a’, ‘aa’, ‘aaa’, but not ‘’.

? - Matches the preceding character zero or one time.

Example:
/a?/ matches ‘’ or ‘a’.

{n} - Matches the preceding character exactly n times.

Example:
/a{3}/ matches ‘aaa’.

{n,} - Matches the preceding character n or more times.

Example:
/a{2,}/ matches ‘aa’, ‘aaa’, ‘aaaa’, etc.

{n,m} - Matches the preceding character between n and m times (inclusive).

Example:
/a{2,4}/ matches ‘aa’, ‘aaa’, or ‘aaaa’.

Grouping and Capturing

() - Groups characters together and captures the matched group.

Example:
/(ab)+/ matches one or more occurrences of ‘ab’.

(?:pattern) - Groups characters without capturing the group.

Example:
/(?:ab)+/ matches one or more occurrences of ‘ab’, but doesn’t capture it.

\1, \2, … - Backreferences to captured groups.

Example:
/(.)(.)\2\1/ matches ‘abba’, ‘xyyx’, etc.

Anchors & Lookarounds

Anchors

^ - Matches the beginning of the string or line (in multiline mode).

Example:
/^start/m matches ‘start’ at the beginning of each line.

$ - Matches the end of the string or line (in multiline mode).

Example:
/end$/m matches ‘end’ at the end of each line.

\b - Matches a word boundary (the position between a word character and a non-word character).

Example:
/\bword\b/ matches ‘word’ as a whole word.

\B - Matches a non-word boundary.

Example:
/\Bword\B/ matches ‘awordb’ but not ‘word’.

Lookarounds

(?=pattern) - Positive lookahead assertion: ensures that the pattern follows, but doesn’t include it in the match.

Example:
/\w+(?=\s)/ matches a word followed by a space, but doesn’t include the space.

(?<=pattern) - Positive lookbehind assertion: ensures that the pattern precedes, but doesn’t include it in the match.

Example:
/(?<=\s)\w+/ matches a word preceded by a space, but doesn’t include the space.

(?!pattern) - Negative lookahead assertion: ensures that the pattern does not follow.

Example:
/\d+(?!\.)/ matches a number not followed by a dot.

(?<!pattern) - Negative lookbehind assertion: ensures that the pattern does not precede.

Example:
/(?<!\.)\d+/ matches a number not preceded by a dot.

Common Regex Patterns

Common Patterns

URL:
(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})

Matches a URL.

Email:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

Validates a basic email format.

IP Address:
\b(?:\d{1,3}\.){3}\d{1,3}\b

Matches an IPv4 address.

Date (YYYY-MM-DD):
\d{4}-\d{2}-\d{2}

Matches a date in YYYY-MM-DD format.

Phone Number (US):
\b\d{3}[-]\d{3}[-]\d{4}\b

Matches a US phone number format (XXX-XXX-XXXX).

HTML Tag:
<[^>]+>

Matches an HTML tag.

cheats tweaks