Skip to content
Innovialabs

Regular Expressions for Beginners: Syntax Cheat Sheet

Basics4 min

In short

A regular expression is a text-matching pattern built from character classes, quantifiers, anchors and groups; learn those four building blocks and you can read most regexes.

What a regular expression is

A regular expression (regex) is a short pattern that describes the text you are looking for. It is used to validate input formats, extract data from strings, and search and replace in code and logs.

Almost every regex is built from four blocks:

  • what to match: characters and character classes;
  • how many times: quantifiers;
  • where: anchors and boundaries;
  • how to group: groups and alternation.

Syntax in JavaScript, Python, PHP, Java and Go is mostly the same, but details differ, so check your language's documentation.

Characters and character classes

PatternMeaningExample match
abcliterally "abc"abc
.any character except a newlinea, 7, %
\da digit5
\wa letter, digit or _a, Z, 3, _
\sa whitespace characterspace, tab
\D, \W, \Sanything except \d, \w, \s
[abc]one of a, b, cb
[a-z]a character in the rangek
[^0-9]any character except digitsx
\.a literal dot, not "any".

The special characters . * + ? ^ $ ( ) [ ] { } | and the backslash must be escaped with \ when you want them literally.

Note that in some engines \w and \d match only Latin letters and ASCII digits by default. For Cyrillic or other scripts, an explicit class or Unicode mode is more reliable.

Quantifiers

PatternHow many times
*0 or more
+1 or more
?0 or 1
{3}exactly 3
{2,5}2 to 5
{2,}2 or more

Quantifiers are greedy by default and grab as much as possible. Add ? to make them lazy: on <b>text</b>, <.+> matches the whole string, while <.+?> matches only <b>.

Anchors and boundaries

PatternMeaning
^start of the string
$end of the string
\bword boundary
\Bnot a word boundary

For validation you almost always need ^ and $. The pattern \d{4} finds 4 digits inside "abc12345", while ^\d{4}$ accepts only a string of exactly four digits.

Groups and alternation

PatternMeaning
(abc)capturing group
(?:abc)non-capturing group
a|ba or b
\1backreference to the first group
(?<name>...)named group (in Python, (?P<name>...))
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const m = "Date: 2026-03-15".match(re);
console.log(m.groups.year); // "2026"

Use (?:...) when a group exists only for a quantifier or alternation, so your capture numbering stays intact.

Lookahead and lookbehind

Lookarounds inspect neighboring text without including it in the match.

PatternMeaning
X(?=Y)X if followed by Y
X(?!Y)X if not followed by Y
(?<=Y)XX if preceded by Y
(?<!Y)XX if not preceded by Y

A classic example is a password check: ^(?=.*\d)(?=.*[a-z]).{8,}$ means at least 8 characters, at least one digit and one lowercase letter. Lookbehind is not supported in every engine, and some restrict its length.

Flags

  • i: case-insensitive;
  • g: find all matches (JavaScript);
  • m: ^ and $ apply to each line of the text;
  • s: the dot also matches a newline;
  • u: Unicode mode (JavaScript).

How to test your regexes

  1. Build the pattern in an online tester such as regex101 or RegExr: they highlight matches and explain every part.
  2. Select the same engine your language uses.
  3. Collect strings that should and should not match, including the empty string and edge cases.
  4. Move that list into unit tests so the regex does not break during later edits.

FAQ

Usually because of escaping: in string literals the backslash must be doubled ("\\d"), or you should use raw strings such as r"\d" in Python. The second common cause is a different engine or different flags.

We can help with this

If you'd rather get the result than figure it all out yourself — this is what we do every day.

Discuss a project

Read next

Have an idea or a task? Tell us about it

We'll reply within one business day and propose a solution. The consultation is free.