1. Home
  2. Guides
  3. Regex cheat sheet: patterns you will actually use
Developer

Regex cheat sheet: patterns you will actually use

Character classes, quantifiers, groups and copy-ready patterns, explained briefly.

12 October 20267 min read

A regular expression (regex) describes a pattern in text: “a number followed by letters”, “anything that looks like an email”. This cheat sheet covers the syntax used by JavaScript, Python, Java and most other languages, followed by ready patterns and the traps that make regexes slow or wrong. Test every pattern as you go with the Regex Tester.

Characters and classes

PatternMatches
.Any character except a new line
\d \DA digit / a non-digit
\w \WA letter, digit or underscore / anything else
\s \SWhitespace / non-whitespace
[abc] [^abc]One of a, b, c / anything but those
[a-z0-9]A range
\. \(A literal dot or bracket (escape special characters with \)

Quantifiers, anchors and groups

PatternMeaning
* + ?0 or more / 1 or more / 0 or 1
{3} {2,5} {2,}Exactly 3 / between 2 and 5 / at least 2
*? +?Lazy: as few as possible
^ $Start / end of the text (or line, with the m flag)
\bA word boundary
(abc)A capturing group
(?:abc)A group that does not capture
a|ba or b
(?=x) (?!x)Followed by x / not followed by x
Try a pattern on your own text and see every match and group highlighted.Open Regex Tester →

Patterns you will actually use

ForPattern
Indian mobile number^[6-9]\d{9}$
PIN code^[1-9]\d{5}$
Date as dd-mm-yyyy^(0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-\d{4}$
Simple email check^[^\s@]+@[^\s@]+\.[^\s@]+$
URL starting http(s)https?:\/\/[^\s]+
Hex colour^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Extra spaces\s{2,}
Do not rely on a regex to fully validate email. The real email rules are far more complex. Use a simple check to catch typos, then send a confirmation message to be sure the address works.

Flags

  • g find all matches, not just the first.
  • i ignore case.
  • m make ^ and $ match at each line.
  • s let . match new lines too.

Mistakes to avoid

  • Greedy surprises. <.*> matches from the first < to the last > on the line. Use <.*?> or <[^>]*>.
  • Forgetting anchors. Without ^ and $, \d{10} matches inside a 12-digit number.
  • Catastrophic backtracking. Nested repeats like (a+)+$ can freeze a program on certain input. Keep patterns simple and test with long, almost-matching text.
  • Parsing HTML or JSON with regex. Use a real parser; try the JSON Formatter to check JSON.

Frequently asked questions

Do all languages use the same regex syntax?

The basics are shared. Differences appear in lookbehind, named groups and Unicode support, so test in the language you use.

How do I replace with a group?

Refer to the group in the replacement, as $1 in JavaScript or \1 in Python, for example turning (\d{2})-(\d{2}) into $2/$1.

More guides

Free for everyone, kept free by you.

No paywall, no sign-up and no watermarks. If a tool saved you time, you can chip in any amount by UPI to keep Nova Free Tools running.

Esc
↑↓ move↵ openEsc close