PlayTools

JavaScript 正则沙盒

实时高亮显示正则匹配结果,支持 g/i/m 标志位和捕获组。

Runs in your browser — nothing you enter is uploaded
Examples:
//

Matches highlighted — 2 matches found

Contact us at hello@example.com or support@tools.io for help.

Match Details

#1hello@example.comidx 14
#2support@tools.ioidx 35

JavaScript 正则沙盒

注意与 PCRE/POSIX 方言差异;g/i/m/s 会显著改变匹配行为,尤其是 s(dotAll)。

分享截图前请脱敏日志中的邮箱、电话等信息。

How the tester works

Type the pattern without surrounding slashes, toggle flags, and paste the text to search. The pattern runs on your browser’s own JavaScript RegExp engine on every keystroke, so what you see is exactly how the same pattern behaves in JavaScript and TypeScript code.

All matches are listed even if the g flag is off — the tester adds it internally for the list — together with each match’s position and its capture groups in order. Matches are highlighted in the text below. Patterns that can match an empty string, such as a* or \b, are stepped through one position at a time, so they show zero-width matches without freezing the page.

Five presets (email, URL, IPv4, date and hex colour) load a working pattern with sample text — useful starting points to adapt rather than definitive validators.

Worked examples

  • Prices: \$\d+(?:\.\d{2})? on “Coffee $4.50, bagel $3, total $7.50” matches $4.50, $3 and $7.50. The dollar sign and the dot are escaped because both are special characters; (?: … ) groups the cents without capturing them.
  • Dates with groups: (\d{4})-(\d{2})-(\d{2}) on “Due 2026-10-01” gives one match with groups 2026, 10 and 01 — in code, a replacement string such as $3/$2/$1 reorders them to 01/10/2026.
  • Whole words: \bcat\b on “cat concatenate cat.” matches the two standalone words and skips “concatenate”.
  • Log lines: ^ERROR.*$ with the m flag matches every line that starts with ERROR. Without m, ^ and $ only match the start and end of the whole text.
  • Greedy vs lazy: <.+> on “<b>bold</b>” matches the entire string, because .+ grabs as much as it can. <.+?> matches “<b>” and “</b>” separately.

Mistakes that cause most regex bugs

  • Unescaped dots: example.com matches “exampleXcom” too. Write example\.com.
  • Missing anchors when validating: \d{5} finds five digits inside “123456789”. For a whole-input check use ^\d{5}$.
  • Catastrophic backtracking: nested quantifiers such as (a+)+$ take exponential time on input like “aaaaaaaaaaaaaaaaaaaaaaaa!”. In a browser that freezes the tab — here as well as in production. Rewrite with a single quantifier — a+$ instead of (a+)+$ — or a more specific character class.
  • Expecting regex to fully validate emails or parse HTML: the email preset accepts typical addresses, not every address RFC 5322 allows, and nested HTML needs a real parser.
  • Forgetting flags: case-sensitive matching is the default — add i if “Error” and “ERROR” should both match.

Moving a pattern to another language

Most everyday syntax — character classes, quantifiers, groups, anchors, lookahead — is identical across JavaScript, Python, Java, C#, PHP and Go. The differences that bite are mostly about escaping and a few advanced features:

  • String escaping: in Java and C# source code every backslash is doubled (“\\d+”); in Python use a raw string, r"\d+".
  • Named groups: JavaScript, .NET and Java write (?<year>\d{4}); Python writes (?P<year>\d{4}).
  • Go (RE2) has no lookahead, lookbehind or backreferences, by design, in exchange for guaranteed linear-time matching.
  • PHP’s preg_ functions need delimiters around the pattern, for example /\d+/i.
  • Lookbehind (?<=…) works in all current browsers and Node.js, but very old browsers lack it.

Privacy

Patterns and test text are processed only in your browser. Log excerpts often contain email addresses, IP addresses and tokens; you can test against them here without them leaving your machine, but scrub them before sharing a screenshot.

In-depth guides

These free tools pair well with this page — open them in a new tab to finish your workflow.

常见问题

这个工具使用什么正则表达式引擎?

此工具使用您浏览器内置的JavaScript RegExp引擎。JavaScript正则支持大多数常见功能:字符类、量词、先行断言、分组和反向引用。它缺少PCRE中的部分功能,如可变长度后顾(旧版浏览器)或原子分组。

正则标志的含义是什么?

常见标志:g(全局——查找所有匹配,而非仅第一个)、i(不区分大小写)、m(多行——^和$匹配每行的开头/结尾)、s(dotAll——.也匹配换行符)、u(Unicode——启用完整Unicode支持)。

如何匹配字面点号或特殊字符?

用反斜杠转义。正则中的点(.)匹配任何字符;要匹配字面点,请写\。同样,要匹配\、(、)、[、]、{、}、*、+、?、^、$、|,在每个字符前加反斜杠。

捕获组和非捕获组有什么区别?

捕获组(括号:())捕获匹配文本并使其作为$1、$2等引用。非捕获组(?:...)在不捕获的情况下分组——当您想应用量词而不捕获内容时很有用。

什么是先行断言和后顾断言?

先行断言(?=...)断言当前位置后必须跟有某模式,不消耗字符。后顾断言(?<=...)断言当前位置前必须有某模式。例如,\d+(?= dollars)匹配后面跟着' dollars'的数字,但不包含' dollars'在匹配结果中。