工具 指南
developer 免费 无需注册

正则表达式测试器和调试器

使用实时匹配高亮、捕获组和替换模式测试正则表达式。

正在加载工具…

关于此工具

交互式正则表达式测试工具。输入模式和测试字符串,实时查看所有匹配项高亮显示。查看捕获组、命名组和匹配位置。切换到替换模式预览替换结果。支持所有JavaScript正则表达式标志(g、i、m、s、u)。

使用方法

  1. 1 在模式字段中输入您的正则表达式。
  2. 2 可选择添加标志(g表示所有匹配,i表示不区分大小写,m表示多行)。
  3. 3 粘贴您的测试字符串——匹配项实时高亮显示。
  4. 4 切换到替换标签以使用$1、$2或命名组预览替换结果。

What a regular expression actually does

A regular expression — a "regex" — is a tiny pattern language for describing the shape of text rather than its exact contents. Instead of searching for the literal word "color", you can write a pattern that matches "color" or "colour", any string of digits, every email-looking token, or each tag in a block of HTML. A regex engine reads your pattern left to right and tries to align it against the test string, one position at a time, backtracking when a branch fails. This tester uses JavaScript's built-in RegExp engine (the ECMAScript flavour), so anything that works here works the same way in Node.js and every modern browser. The pattern and your test text are evaluated entirely in your browser — nothing is uploaded.

The building blocks

Most patterns combine a small set of pieces:

PieceMeaningExample
\d \w \sA digit, word character, or whitespace\d\d matches "42"
[abc]Any one character in the set[aeiou] matches a vowel
+ * ?One-or-more, zero-or-more, optionala+ matches "aaa"
{2,4}Between 2 and 4 repetitions\d{3,4} matches "1234"
(…)A capture group you can extract(\d+)-(\d+)
|Alternation (either side)cat|dog

The two slashes shown on either side of the pattern field are the regex delimiters — you type only the pattern between them. The small box to the right holds the flags.

Flags, and one thing this tester does for you

Flags change how the whole pattern behaves. i makes matching case-insensitive, m makes ^ and $ anchor to each line instead of the whole string, s lets . match newlines, and u turns on full Unicode handling. The g (global) flag finds every match instead of stopping at the first. This tester always adds g for you internally, so the highlighter and match list show all occurrences even if you forget it — useful, but remember that in your own code, without g, only the first match is returned.

A worked example

Say you want to pull the year and month out of dates like 2026-06-09. Type the pattern (?<year>\d{4})-(?<month>\d{2}) and paste a paragraph containing several dates. As you type, the tester highlights each match in a rotating set of colours and shows a running count. The Match Details panel lists every match with its character index range, the exact matched value, the numbered capture groups ($1 = the year, $2 = the month), and the named groups (year and month) spelled out. Seeing the index range is what makes this a debugger rather than a search box: you can tell exactly where in the string each match landed and why two near-matches behaved differently.

Replace mode

Switch to the Replace tab to preview substitutions without changing your data. The replacement string supports the same back-references as JavaScript's String.replace: $1, $2 insert captured groups, $& inserts the whole match, and $<name> inserts a named group. For example, with pattern (\w+)@(\w+) and replacement $2 [at] $1, the text "ada@example" becomes "example [at] ada". The result updates live, so you can tune the pattern and the replacement together before pasting either into your editor or script.

Where regex earns its keep

Reach for a regex when the pattern is regular but the exact text varies. Common jobs include validating that a field looks like an email or phone number, extracting every URL or hashtag from a block of prose, splitting a log line into its timestamp and message, find-and-replacing across many variations at once, and stripping or reformatting markup. The test-and-replace workflow here mirrors those tasks: build the pattern in Test mode until the highlight covers exactly what you intend and nothing more, confirm the capture groups in the details panel, then move to Replace mode to shape the output. A useful discipline is to test against negative examples too — paste in strings that should not match and confirm the count stays at zero, since an over-eager pattern that matches too much is a more common bug than one that matches too little.

Common mistakes

  • Greedy quantifiers swallowing too much. .* matches as much as possible, so <.*> on <a><b> grabs the whole thing. Add a ? to make it lazy: <.*?> stops at the first >.
  • Forgetting to escape special characters. A literal dot, plus, or parenthesis must be escaped: write \. to match a real period, since a bare . matches almost any character.
  • Anchors without the right flag. ^ and $ match the start and end of the whole string by default; add the m flag if you mean the start and end of each line.
  • Assuming lookbehind everywhere. JavaScript supports lookbehind (?<=…), and so does this tester, but some other engines do not — check your target language before relying on it.
  • Catastrophic backtracking. Nested quantifiers like (a+)+ against a long non-matching string can hang the engine. Keep patterns specific and avoid overlapping repetition.

常见问题

{# Alpine.js — self-hosted. (The previous jsdelivr CDN tag had a stale SRI integrity hash, so the browser refused to run it and window.Alpine was never defined — silently breaking every FAQ accordion and Alpine tool.) #}