A regular expression (regex) describes a text pattern: “a run of digits”, “a word at the start of a line”, “a date in YYYY-MM-DD format”. It is used to validate input, extract data from text or make bulk replacements. The syntax looks cryptic at first, but about fifteen symbols cover most needs.

The examples in this guide use JavaScript syntax, the one used by the tester, web browsers and Node.js. It is very close to Python, PHP (PCRE) and Java; the differences are listed at the end.

The building blocks

Characters and classes

PatternMeaningExample
abcthe literal text “abc”finds “abc” in “xabcx”
.any character except a line breakc.t finds “cat”, “cut”, “c-t”
\d / \Da digit 0–9 / anything but a digit\d\d finds “42”
\w / \Wan unaccented letter, a digit or _ / the opposite\w+ splits “café” into “caf”
\s / \Sa space, tab or line break / the opposite\s+ finds each run of whitespace
[ae]one character from the listgr[ae]y finds “gray” and “grey”
[a-z0-9]one character in the ranges[A-F0-9]: one upper-case hex digit
[^0-9]one character not in the list[^,]+: everything up to the next comma
\. \( \?the special character itself (escaped)3\.14 finds “3.14” but not “3x14”

Characters to escape with a backslash: . * + ? ^ $ { } ( ) | [ ] \ /.

Quantifiers

PatternRepetitionsExample
*0 or morebo*m: “bm”, “bom”, “booom”
+1 or more\d+: “7”, “2026”
?0 or 1 (optional)colou?r: “color” and “colour”
{3}exactly 3\d{3}: “075”
{2,4}2 to 4\d{2,4}: “12”, “2026”
{2,}2 or more\s{2,}: double spaces

Anchors and alternation

  • ^ and $: start and end of the text (or of each line with the m flag). Essential to validate a whole input: ^\d{5}$ accepts “90210” but rejects “90210 Beverly Hills”.
  • \b: a word boundary. \bcat\b finds “cat” but not “catalog”.
  • |: alternation. dog|cat finds either.

Groups and captures

Parentheses group part of the pattern, to apply a quantifier to it or to capture what it matched.

  • (\d{2})/(\d{2})/(\d{4}) on “on 11/08/2026” captures “11”, “08” and “2026” in groups 1, 2 and 3.
  • Named groups: (?<month>\d{2})/(?<day>\d{2})/(?<year>\d{4}) captures the same values, available by name.
  • Non-capturing group: (?:-\d{4})? groups without creating a capture, handy for an optional part.
  • Replacement: in JavaScript, "11/08/2026".replace(/(\d{2})\/(\d{2})\/(\d{4})/, "$3-$1-$2") gives “2026-11-08”.

Lookarounds check what surrounds a match without including it: (?<=\$)\d+(?:\.\d{2})? finds an amount preceded by “$”. On “Total: $25 or $9.99” it returns “25” and “9.99”.

Flags

FlagEffect
gall matches, not just the first
iignore case: /cat/i finds “Cat” and “CAT”
m^ and $ apply to each line
sthe dot . also matches line breaks
uUnicode mode: required for \p{L} (any letter, accents included) and emoji

Ready-to-use examples

NeedPatternNote
US ZIP code^\d{5}(?:-\d{4})?$“90210” and “90210-1234”; does not check that the code exists
US phone number^\(?\d{3}\)?[ .-]?\d{3}[ .-]?\d{4}$accepts “(555) 123-4567” and “555.123.4567”
ISO date YYYY-MM-DD^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$still accepts 2026-02-31: check the date in code
Hex colour^#?([0-9a-f]{3}|[0-9a-f]{6})$ + i“#1A2B3C”, “fff”
Email address (basic check)^[^\s@]+@[^\s@]+\.[^\s@]+$catches typos; only a confirmation email proves an address exists
Whole word, any case\bcat\b + gion “Cat, catalog, CAT”: “Cat” and “CAT”
Multiple spaces\s{2,} + greplace with a single space

To tidy up text (double spaces, empty lines, letter case), the text cleaner does these replacements without writing any regex.

The most common pitfalls

1. Greedy quantifiers

By default, * and + take as many characters as possible. On “<b>bold</b>”, <.+> matches the whole string. Add ? for the “lazy” version: <.+?> matches “<b>” then “</b>”. To parse real HTML, use an HTML parser instead: regexes cannot handle nested tags.

2. Forgetting anchors when validating

Without ^ and $, \d{5} also “validates” “abc123456xyz”, because it finds five digits in it.

3. Accents and \w

\w only knows the letters A to Z without accents: ^\w+$ rejects “café”. In JavaScript, use ^\p{L}+$ with the u flag.

4. An unescaped dot

example.com also accepts “exampleXcom”. Write example\.com.

5. Runaway patterns

Nested quantifiers such as (a+)+$ can force the engine to try an exponential number of combinations on a text that does not match, freezing the page or a server (known as “ReDoS”). Avoid repeating a group that itself repeats the same characters.

From one language to another

  • Delimiters: JavaScript writes /pattern/flags, PHP '/pattern/i' inside a string, Python re.compile(r"pattern", re.I). In the tester, enter the pattern without the slashes.
  • Named groups: (?<name>…) in JavaScript and PHP; (?P<name>…) with Python’s re module.
  • Replacement: $1 in JavaScript and PHP, \1 in Python.
  • The g flag does not exist in Python or PHP, where you pick the function instead (findall, preg_match_all).

Frequently asked questions

Why does my pattern find only one result?

The g flag is missing. Without it, the search stops at the first match.

How do I search for a special character like “?” or “+”?

Escape it with a backslash: \?, \+. Inside square brackets, most special characters lose their meaning: [.?+] finds a dot, a question mark or a plus sign.

Can a regex reliably validate an email address?

No. The standard allows rare forms that no readable pattern covers, and a well-formed address may not exist. A basic check followed by a confirmation email is the reliable method.