正規表現チートシートとテスター
ライブパターンテストとマッチハイライト付きのインタラクティブな正規表現リファレンス。
このツールについて
アンカー、文字クラス、量指定子、グループ、特殊文字を網羅した包括的なカテゴリ別正規表現リファレンスを閲覧できます — 各エントリには記号、名前、説明、実際の例が含まれています。上部のクイックテスターを使用してテキストとパターンを入力し、リアルタイムでハイライト表示されたマッチを確認できます。ワンクリックコピーで任意のパターンをクリップボードに直接コピーできます。
使い方
- 1 上部のテキストエリアにテスト文字列を入力または貼り付けてください。
- 2 必要に応じて正規表現パターンとフラグ(g、i、m、s)を入力してください。
- 3 マッチはリアルタイムでハイライト表示されます。マッチ数は入力の下に表示されます。
- 4 下のリファレンステーブルを参照し、任意のエントリの「コピー」をクリックしてそのパターンをコピーしてください。
What a regular expression really is
A regular expression (regex) is a compact pattern that describes a set of strings. Instead of searching for one literal word, you describe its shape — "three digits," "a word at the start of a line," "anything between angle brackets" — and the engine finds every piece of text that fits. This page is two things at once: a categorised cheat sheet of the building blocks, and a live tester where you type a pattern and watch it highlight matches in your own text in real time. Learning regex by reading definitions is slow; learning it by seeing a pattern light up exactly what you expected is fast.
The five families of building blocks
Every regex is assembled from a small vocabulary. The cheat sheet groups them into five categories, each with a runnable example you can copy:
- Anchors match positions, not characters:
^(start),$(end), and\b(word boundary).\bcat\bmatches "cat" in "the cat sat" but not the "cat" inside "concatenate." - Character classes match one character from a set:
\d(digit),\w(word character),\s(whitespace),[aeiou](any listed character),[^...](anything not listed), and.(any character except newline). - Quantifiers say how many:
*(zero or more),+(one or more),?(optional), and{2,4}(between two and four). Their lazy versions, like*?, match as few characters as possible. - Groups and lookarounds:
(...)captures,(?:...)groups without capturing, and lookahead/lookbehind such as(?<=\$)\d+match a position based on what surrounds it without consuming those characters. - Special characters: alternation
|(or), plus escapes for newline\n, tab\t, and carriage return\r.
Greedy versus lazy: the classic trap
This is the single most common source of regex confusion. By default quantifiers are greedy — they grab as much as they can. Run <.*> against <b>bold</b> in the tester and it matches the entire string, from the first < to the last >, because .* swallowed everything in between. Add a single ? to make it lazy — <.*?> — and it now matches just <b>, then </b> separately, because the engine stops at the first > it can. Seeing both behaviours highlighted side by side makes the difference click instantly.
Flags change the rules
The tester exposes the four flags you toggle most:
| Flag | Effect |
|---|---|
g | Global — find all matches, not just the first (on by default here so the tester can highlight every hit). |
i | Case-insensitive — cat also matches "Cat" and "CAT". |
m | Multiline — ^ and $ match the start and end of each line, not just the whole string. |
s | Dotall — . also matches newline characters. |
A worked example: validating a price
Suppose you want every dollar amount in a sentence. Paste The shirt is $25 and the hat is $9 into the test string and type the pattern \$\d+. The \$ escapes the literal dollar sign (an unescaped $ would mean "end of string"), and \d+ grabs one or more digits after it. The tester highlights $25 and $9 and reports "2 matches." Want only the number, not the sign? Use a lookbehind: (?<=\$)\d+ matches the digits while leaving the $ out of the highlight.
Common mistakes the tester helps you catch
- Forgetting to escape special characters. A literal dot, dollar, question mark, or parenthesis must be written
\.,\$,\?,\(. An unescaped one means something else entirely. - Greedy matches that overreach. If your pattern grabs far more than intended, reach for the lazy version first.
- Assuming
.matches everything. By default it skips newlines — add thesflag if your text spans multiple lines. - Invalid syntax. If your pattern won't compile, the tester shows the exact error message in red instead of silently failing, so you can fix the typo on the spot.
Privacy
The tester uses your browser's own built-in regex engine (the same one JavaScript uses), so every pattern and every test string is evaluated locally. Nothing you type — neither your patterns nor the text you test them against — is sent to or stored on any server.