跳至主要內容
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

請將尖括號內的任何內容替換為你自己的數值。指令均源自官方文件,且範例均在所示版本上實際執行。