设置税率,添加任何备注,然后点击"打印/另存为PDF"导出。
正则表达式速查表与测试工具
关于此工具
交互式正则表达式参考,支持实时模式测试和匹配高亮。
使用方法
- 1 不会——所有匹配完全在浏览器中使用内置的JavaScript RegExp引擎运行。
- 2 在顶部的文本区域中输入或粘贴测试字符串。
- 3 输入正则表达式模式,并根据需要选择标志(g、i、m、s)。
- 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.