QuikTool

Regex cheat sheet for beginners

A regular expression, or regex, is a short pattern that describes text. You use it to find, check or replace things such as dates, IDs and email-like strings. The symbols look cryptic at first, but a small set covers most jobs.

Try it now: Regex Tester.

The core symbols

PatternMatches
.Any one character except a line break
\d \w \sA digit, a word character (letter, digit, underscore), a whitespace character
\D \W \SThe opposite of each of those
[abc] [a-z] [^0-9]One of a, b or c; a lowercase letter; anything except a digit
^ $The start and the end of the text (or of a line, with the m flag)
\bA boundary between a word and a non-word character
a|ba or b

Repeating things

PatternMatches
*Zero or more of the previous item
+One or more
?Zero or one (makes it optional)
{3} {2,4} {2,}Exactly 3; between 2 and 4; 2 or more
*? +?The lazy versions: match as little as possible

Groups and lookaheads

Parentheses make a group, which treats several items as one and captures the matched text so you can reuse it. (ab)+ matches ab, abab and so on. Write (?:ab) for a group that does not capture, and (?<year>\d{4}) to give a capture a name. A lookahead such as \d+(?= dollars) matches the digits only when " dollars" follows, without including those words in the match.

Flags

Flags change how the whole pattern behaves. In JavaScript, g finds every match instead of the first, i ignores upper and lower case, m makes ^ and $ work on each line, s lets the dot match line breaks, and u turns on full Unicode support.

Patterns you can reuse

GoalPattern
A date like 2026-10-07^\d{4}-\d{2}-\d{2}$
A hex color^#(?:[0-9a-fA-F]{3}){1,2}$
Extra spaces at the ends (with the g flag)^\s+|\s+$
A whole number, with optional sign^-?\d+$

Building a pattern step by step

Suppose you want to find prices such as $4.99 in a block of text. Start with what must be there: a dollar sign, which is special in a regex, so write \$. Then one or more digits, \d+. Then a literal dot, \., and exactly two digits, \d{2}. Joined together, the pattern is \$\d+\.\d{2}. Add the g flag to find every price, then paste a sample into a regex tester and watch what it highlights. If it misses $5 with no cents, make the cents optional by wrapping them in a group followed by a question mark.

Mistakes to avoid

  • Forgetting to escape special characters. To match a literal dot, write \. because . means any character.
  • Greedy matching. Run <b>.*</b> against the text <b>one</b> and <b>two</b> and it matches everything from the first <b> to the last </b>; the lazy <b>.*?</b> stops at the first </b>.
  • Using a regex for everything. Email addresses and HTML are too irregular for a short pattern, so use a real parser or a library when it matters.
  • Patterns that can take forever. Nested repeats such as (a+)+$ can make the engine try an enormous number of paths on a failing input.

Frequently asked questions

Do all programming languages use the same regex?
The basics are the same everywhere, but the details differ. JavaScript, Python, PHP and others each support slightly different features, so test the pattern in the flavor you will use.
What does the g flag do?
It stands for global. Without it a search stops at the first match, and with it you get every match in the text.
How do I test a regex safely?
Paste your pattern and sample text into a regex tester that shows each match as you type. Try inputs that should fail as well as ones that should match.

Try the tools