正则表达式测试器 — 在线测试正则表达式
这个正则表达式测试器支持实时编写、测试和调试正则表达式。输入模式和测试字符串,即可查看带有高亮匹配项、捕获组和匹配详情的实时结果,适用于 JavaScript 正则表达式。
Regex Tester
Test and debug regular expressions in real time with match highlighting and capture group details
Frequently Asked Questions
什么是正则表达式(regex)?
正则表达式(regex或regexp)是定义搜索模式的字符序列。它用于字符串内的模式匹配——查找符合特定结构的文本、验证输入格式以及执行搜索替换操作。几乎每种编程语言都支持正则表达式,包括JavaScript、Python、Java、Go和许多文本编辑器。
最常用的正则表达式模式是什么?
最常用的正则表达式模式包括:邮件验证([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})、URL匹配(https?://[\w.-]+)、电话号码(\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4})、IP地址、ISO格式日期(\d{4}-\d{2}-\d{2})、十六进制颜色代码(#[0-9a-fA-F]{3,6})以及整数或小数匹配。
什么是正则表达式中的捕获组?
捕获组是正则表达式模式中用括号括起来的部分()。当正则表达式匹配时,每个组捕获其匹配的文本,可以在后面引用。例如,(\d{4})-(\d{2})-(\d{2})匹配"2026-03-22"时创建三个捕获组:$1="2026"、$2="03"、$3="22"。命名组使用语法(?<name>...)使代码更清晰。非捕获组(?:...)进行分组但不捕获。
什么是前瞻和后顾断言?
前瞻和后顾是零宽度断言,在不将其包含在匹配中的情况下检查模式。正向前瞻(?=abc)匹配后面跟着"abc"的位置。负向前瞻(?!abc)匹配后面不跟"abc"的位置。正向后顾(?<=abc)匹配前面是"abc"的位置。负向后顾(?<!abc)匹配前面不是"abc"的位置。它们对于复杂的条件匹配非常有用。
贪婪匹配和懒惰匹配有什么区别?
贪婪量词(*、+、?)尽可能多地匹配文本,而懒惰量词(*?、+?、??)尽可能少地匹配。例如,给定字符串"<b>bold</b>",贪婪模式<.*>匹配整个字符串"<b>bold</b>",但懒惰模式<.*?>只匹配"<b>"。当您想要最短可能的匹配时,使用懒惰量词。
什么是灾难性回溯?
当正则表达式引擎需要指数级时间来确定字符串不匹配某个模式时,会发生灾难性回溯。这发生在嵌套量词如(a+)+或具有重叠替代项的模式中。引擎在失败前尝试每种可能的组合,导致极度缓慢或卡死。通过使用具体字符类、原子组和限制重复范围来避免这种情况。
正则表达式标志(修饰符)有什么作用?
正则表达式标志修改模式的解释方式。常见标志包括:g(全局)——查找所有匹配而不仅仅是第一个;i(不区分大小写)——忽略大小写差异;m(多行)——^和$匹配行边界而不是字符串边界;s(dotall)——.字符匹配换行符;u(Unicode)——启用完整Unicode匹配。标志可以组合使用,例如/pattern/gim。
如何在线测试正则表达式?
在模式字段中输入正则表达式,在下方的文本区域粘贴测试字符串。匹配结果会在您输入时实时高亮显示。使用标志按钮切换标志(g、i、m、s、u)。在匹配面板中查看包括索引位置和捕获组在内的匹配详情。使用替换部分测试替换模式,并浏览常见模板以获取预置模式。
正则表达式替换中的反向引用如何工作?
在正则表达式替换中,反向引用允许您在替换字符串中重用捕获组。使用$1、$2等引用编号捕获组,或$<name>引用命名组。例如,将(\w+)\s(\w+)替换为$2 $1会交换两个单词。在替换字符串中,$&引用整个匹配,$`引用匹配前的文本,$'引用匹配后的文本。
正则表达式和通配符模式有什么区别?
通配符模式是主要用于Shell中文件路径匹配的简单通配符模式(例如*.txt、src/**/*.js)。它们支持*(任意字符)、?(单个字符)和[...](字符类)。正则表达式更为强大,支持量词、锚点、分组、交替、前瞻和后顾以及反向引用。正则表达式可以操作任何字符串内容,而通配符通常限于路径匹配。
About Regular Expressions
Regular expressions (regex) are powerful patterns used to match, search, and manipulate text. They form a concise language for describing character sequences: from simple literal matches to complex patterns involving repetition, alternation, grouping, and lookaround assertions. Regex is supported in virtually every programming language and text editor, making it an essential tool for developers, data analysts, and system administrators.
This regex tester lets you write, test, and debug regular expressions in real time. As you type your pattern and test string, matches are highlighted instantly, capture groups are extracted, and execution time is measured. You can also perform find-and-replace operations using backreferences.
Regex Syntax Quick Reference
Character Classes
| Pattern | Description |
|---|---|
| [abc] | Match any one of a, b, or c |
| [^abc] | Match any character except a, b, or c |
| [a-z] | Match any character in the range a to z |
| \d | Match any digit (equivalent to [0-9]) |
| \D | Match any non-digit |
| \w | Match any word character (letters, digits, underscore) |
| \W | Match any non-word character |
| \s | Match any whitespace character (space, tab, newline) |
| \S | Match any non-whitespace character |
| . | Match any character except newline (unless s flag is set) |
Quantifiers
| Pattern | Description |
|---|---|
| * | Match 0 or more times (greedy) |
| + | Match 1 or more times (greedy) |
| ? | Match 0 or 1 time (greedy) |
| {n} | Match exactly n times |
| {n,} | Match n or more times |
| {n,m} | Match between n and m times |
| *? | Match 0 or more times (lazy / non-greedy) |
| +? | Match 1 or more times (lazy / non-greedy) |
Anchors
| Pattern | Description |
|---|---|
| ^ | Match start of string (or line with m flag) |
| $ | Match end of string (or line with m flag) |
| \b | Match word boundary |
| \B | Match non-word boundary |
Groups & Lookaround
| Pattern | Description |
|---|---|
| (abc) | Capture group — matches "abc" and stores the result |
| (?:abc) | Non-capturing group — groups without storing |
| (?<name>abc) | Named capture group |
| \1 | Backreference to capture group 1 |
| a|b | Alternation — match a or b |
| (?=abc) | Positive lookahead — matches if followed by "abc" |
| (?!abc) | Negative lookahead — matches if not followed by "abc" |
| (?<=abc) | Positive lookbehind — matches if preceded by "abc" |
| (?<!abc) | Negative lookbehind — matches if not preceded by "abc" |
Common Regex Patterns
Here are regex patterns frequently used in real-world applications:
- Email validation —
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}matches standard email addresses - URL matching —
https?://[\w.-]+(?:\.[\w.-]+)+[\w.,@?^=%&:/~+#-]*matches HTTP and HTTPS URLs - IP address —
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\bvalidates IPv4 addresses - Date (ISO) —
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])matches YYYY-MM-DD dates - Hex color —
#(?:[0-9a-fA-F]{3}){1,2}\bmatches #RGB and #RRGGBB color codes - Password strength —
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}requires uppercase, lowercase, digit, and special character (8+ chars)
Regex in Different Languages
While the core regex syntax is broadly similar across languages, there are notable differences in features, escaping rules, and API usage:
| Feature | JavaScript | Python | Java | Go |
|---|---|---|---|---|
| Engine | V8/SpiderMonkey | re module | java.util.regex | RE2 |
| Lookaround | Yes | Yes | Yes | No |
| Named groups | (?<name>...) | (?P<name>...) | (?<name>...) | (?P<name>...) |
| Backreference | \1 or $1 | \1 or \g<1> | \1 or $1 | Not supported |
| Backtracking | Yes | Yes | Yes | No (RE2 is linear) |
| Example | /\d+/g.test(s) | re.findall(r'\d+', s) | Pattern.compile("\\d+") | regexp.MustCompile(`\d+`) |
Regex Performance Tips
Poorly written regex patterns can cause catastrophic backtracking, where the engine takes exponential time to process certain inputs. Follow these tips to write efficient patterns:
- Avoid nested quantifiers — Patterns like
(a+)+or(a*)*cause exponential backtracking on non-matching inputs. Simplify toa+ - Use atomic groups or possessive quantifiers — Where supported,
(?>...)prevents the engine from backtracking into a group - Be specific with character classes — Use
[a-z]instead of.when you know which characters to expect. Narrower classes reduce unnecessary matching attempts - Anchor your patterns — Add
^and$anchors when matching complete strings to prevent the engine from trying every position - Prefer non-greedy quantifiers when appropriate —
.*?stops at the first match rather than consuming the entire string and backtracking - Limit repetition bounds — Use
{1,100}instead of+to cap the number of iterations and prevent runaway matching - Test with adversarial inputs — Always test your regex with long strings of nearly-matching text to identify potential backtracking issues before deploying to production
Related Tools
- Case Converter — Convert text between UPPER, lower, Title, camelCase, and more
- Word Counter — Count words, characters, sentences, and paragraphs in your text
- URL Encoder & Decoder — Percent-encode and decode URLs for web applications