跳转到主要内容
kitlocal.dev

正则表达式速查表

本地运行

100% 在你的浏览器中处理。输入和输出绝不会离开你的设备。

正则表达式语法与可复用配方,附带交互式实时匹配实验室:锚点、字符类、量词、环视断言与修饰符 (ES2024)。

工作原理

A regex engine scans left-to-right trying to satisfy your pattern at each position. Quantifiers (*, +, {n,m}) are greedy by default (consuming as much as possible before backtracking); append `?` to make them lazy, and anchor with `^`/`$` or `\b` when you need exact boundaries.

生存指南

最先需要掌握的少数几个命令。

41 条命令

键盘快捷键/聚焦搜索框JK移动选择C复制选中命令F收藏选中项Esc清空搜索

Characters & character classes

Match single characters by category (`\d`, `\w`, `\s`) or explicit sets (`[a-f0-9]`). Uppercase shorthands invert the match.

官方文档:Character classes
  • 基础必备

    Match a digit, word character ([A-Za-z0-9_]) or whitespace.

    \d  \w  \s

    适用场景

    Core shorthand character classes used in almost every pattern.

    命令解析

    \d
    ASCII digit [0-9] (uppercase \D = non-digit)
    \w
    alphanumeric + underscore (uppercase \W = non-word)
    \s
    space, tab or newline (uppercase \S = non-whitespace)

    示例

    Matches dates inside `"release 2026-11-15"`.

    \d{4}-\d{2}-\d{2}

    运行结果

    ["2026-11-15"]

    另请参见

Anchors & boundaries

Anchors match positions (start/end of string, line or word) rather than consuming characters.

官方文档:Assertions

Quantifiers & laziness

Control how many times the preceding token repeats. Add `?` after any quantifier to match as few times as possible.

官方文档:Quantifiers

Groups, alternation & backreferences

Parentheses group sub-expressions for quantifiers/alternation and capture text for extraction or backreferences.

官方文档:Groups and backreferences

Lookaheads, lookbehinds & flags

Check what appears ahead or behind the current position without making it part of the matched text.

官方文档:Assertions & flags

在 KitLocal 工具中试用

术语表
Greedy vs Lazy
`.*` grabs to the last possible match; `.*?` stops at the earliest match.
Lookaround
Zero-width assertion (`(?=...)`, `(?!...)`, `(?<=...)`) that checks surrounding text without consuming characters.
Backtracking
When a quantifier gives up matched characters so later tokens can match; nested `*`/`+` can cause ReDoS.
Zero-width
Tokens like `^`, `$`, `\b` and lookarounds that match a position rather than characters.

核对时间:2026年11月15日 · 核对版本:ES2024 RegExp

来源: MDN Regular Expressions Guide · MDN RegExp Reference

请将尖括号内的内容替换为你自己的实际值。所有命令均来自官方文档,示例均在标明版本上实测运行。