Kerncode

Uncategorized

Regex Cheat Sheet: Syntax, Quantifiers, Flags & Examples

This regex cheat sheet is a quick reference for the most commonly used regular expression syntax. It covers character classes, quantifiers, anchors, groups, and flags, plus ready-to-copy patterns.

If you want to test any pattern live, use our tool: Regex Tester.

Regex basics

Literal characters

hello

Matches the exact text hello.

Any character

.

Matches any character (except newline unless using the s flag).

Character classes

Digit, non-digit

\d   \D

\d matches a digit (0–9). \D matches anything that is not a digit.

Word, non-word

\w   \W

\w matches letters, digits, and underscore. \W matches anything else.

Whitespace, non-whitespace

\s   \S

\s matches spaces, tabs, and line breaks. \S matches anything that is not whitespace.

Custom sets and ranges

[abc]   [a-z]   [A-Z]   [0-9]

Matches one character from the set or range.

Negated sets

[^abc]

Matches any character except a, b, or c.

Anchors

Start and end of string

^text$

^ anchors to the start. $ anchors to the end.

Word boundary

\bword\b

Matches whole words only (not substrings).

Quantifiers

Common quantifiers

*   +   ?   {n}   {n,}   {n,m}
  • * = 0 or more
  • + = 1 or more
  • ? = 0 or 1
  • {n} = exactly n
  • {n,} = at least n
  • {n,m} = between n and m

Greedy vs lazy

.*      .*?

.* is greedy. .*? is lazy (stops as soon as possible).

Groups and alternation

Capturing groups

(abc)

Captures abc as a group.

Non-capturing groups

(?:abc)

Groups without capturing.

Alternation (OR)

cat|dog

Matches cat or dog.

Backreferences

(["']).*?\1

Matches text wrapped in the same quote character (single or double).

Lookarounds (JavaScript)

Lookbehind support depends on the browser/runtime. Test with the Regex Tester.

Positive lookahead

foo(?=bar)

Matches foo only if followed by bar.

Negative lookahead

foo(?!bar)

Matches foo only if NOT followed by bar.

Positive lookbehind

(?<=foo)bar

Matches bar only if preceded by foo.

Negative lookbehind

(?<!foo)bar

Matches bar only if NOT preceded by foo.

Regex flags (JavaScript)

  • g = global (find all matches)
  • i = case-insensitive
  • m = multiline (^ and $ work per line)
  • s = dot matches newline
  • u = unicode
  • y = sticky

Common regex patterns

Email

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}$

URL

^(https?:\/\/)?([\w-]+\.)+[\w-]+(\/[^\s]*)?$

Phone number

^\+?[0-9][0-9\s().-]{7,}$

IPv4

^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$

Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Numbers only

^\d+$

Test patterns online

Copy any pattern above and test it instantly here:

Open Regex Tester →

Kerncode

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

Uncategorized

What is a UUID? Meaning, Format, Versions & Examples

A UUID (Universally Unique Identifier) is a 128-bit value used to uniquely identify objects, records, sessions, and resources in software
Uncategorized

UUID vs GUID: What’s the Difference and When to Use Each

UUID and GUID are terms used to describe unique identifiers in software systems. In most practical cases, they refer to