.*

Regex Tester

Test regular expressions and inspect every match

0Match count
—

Regex Tester — what it does

Test a regular expression against sample text with the flags you choose, and see every match along with a count. Writing a regex without testing it is guesswork; seeing exactly what it matches — and what it unexpectedly also matches — is the only way to trust one.

Worked example

The pattern ([a-z.]+)@example\.com with flags gi run against three header lines of an email. The output lists every match with its 0-based character offset and the captured group; Match count shows 3.

Input — test text (pattern ([a-z.]+)@example\.com, flags gi)

From: Ann Lee <ann.lee@example.com>
Cc: finance@example.com, Bob Tran <b.tran@example.org>
Reply-To: helpdesk@example.com

Output — matches

[
  {
    "match": "ann.lee@example.com",
    "index": 15,
    "groups": [
      "ann.lee"
    ]
  },
  {
    "match": "finance@example.com",
    "index": 40,
    "groups": [
      "finance"
    ]
  },
  {
    "match": "helpdesk@example.com",
    "index": 101,
    "groups": [
      "helpdesk"
    ]
  }
]

b.tran@example.org is skipped because the domain differs; index counts characters from the start of the whole text, including line breaks.

When this tool helps

  • Building a validation pattern for an email, phone number or reference code.
  • Checking a search-and-replace pattern before running it across a codebase.
  • Understanding what an inherited regular expression actually does.

How to use it

  1. Enter the regular expression.
  2. Set the flags, such as global and case-insensitive.
  3. Paste the test text.
  4. Process and review the matches and count.

Accuracy, limits, and good practice

  • Test the negatives as hard as the positives: the dangerous regex is not the one that misses a valid input but the one that accepts an invalid one.
  • The global flag returns every match rather than only the first, which is usually what you want when checking coverage.
  • Nested quantifiers can cause catastrophic backtracking on certain inputs — if a pattern hangs on a long string, that is why.

Frequently asked questions

Which regex dialect is this?

JavaScript's, which differs in places from PCRE and Python. Lookbehind and named groups are supported in current browsers.

How do I match a literal dot?

Escape it with a backslash. An unescaped dot matches almost any character, which is the single most common beginner error.

Should I validate emails with a regex?

Only loosely. The full specification is impractical to match, so check for a plausible shape and confirm by sending mail.

Why do I get every match even without the g flag?

The g flag is added automatically if you leave it out, and only the letters d, g, i, m, s, u, v and y are kept from the flags field — anything else is dropped silently. The tester stops after 1,000 matches, and a pattern that can match an empty string (such as a*) is advanced one character at a time so it cannot loop forever. When nothing matches, the output reads "No matches." and Match count shows 0.

What do index and groups mean in the output?

index is the 0-based position of the match's first character in the test text, with each line break counting as one character. groups lists the capture groups in order; a group that did not take part in the match appears as null, and a pattern without parentheses shows an empty list.

Related tools on this site

Everything above runs inside this page. Regex Tester needs no account, no upload, and no server round trip — close the tab and nothing is left behind.

正規表示式測試器能做什麼

用你選的旗標,對測試文字驗證正規表示式,並看到每一個符合結果與總數。沒測過的 regex 只是猜測;看清楚它到底命中什麼——以及意外還命中了什麼——才是敢用它的唯一途徑。

實際範例

樣式 ([a-z.]+)@example\.com 搭配旗標 gi,對一封郵件的三行標頭測試。清單列出每個符合項、從 0 起算的字元位置,以及擷取到的群組;符合數顯示 3。

來源:測試文字(樣式 ([a-z.]+)@example\.com,旗標 gi)

From: Ann Lee <ann.lee@example.com>
Cc: finance@example.com, Bob Tran <b.tran@example.org>
Reply-To: helpdesk@example.com

符合項清單

[
  {
    "match": "ann.lee@example.com",
    "index": 15,
    "groups": [
      "ann.lee"
    ]
  },
  {
    "match": "finance@example.com",
    "index": 40,
    "groups": [
      "finance"
    ]
  },
  {
    "match": "helpdesk@example.com",
    "index": 101,
    "groups": [
      "helpdesk"
    ]
  }
]

b.tran@example.org 因網域不同而被略過;index 從整段文字的開頭起算字元數,換行也算在內。

什麼時候用得上

  • 為電子郵件、電話號碼或參考代碼建立驗證樣式。
  • 在整個程式庫執行搜尋取代之前,先驗證樣式。
  • 搞懂接手來的正規表示式究竟在做什麼。

使用步驟

  1. 輸入正規表示式。
  2. 設定旗標,例如全域與忽略大小寫。
  3. 貼上測試文字。
  4. 執行並檢視符合結果與數量。

精確度、限制與實務建議

  • 反例要測得跟正例一樣兇:危險的不是漏掉合法輸入的 regex,而是接受非法輸入的那個。
  • 全域旗標會回傳所有符合處而非只有第一個,檢查覆蓋率時通常正是你要的。
  • 巢狀量詞在某些輸入上會造成災難性回溯——樣式在長字串上卡住,原因就在這裡。

常見問題

這是哪一種 regex 方言?

JavaScript 的,與 PCRE 和 Python 在某些地方不同。現行瀏覽器支援後行斷言與具名群組。

怎麼比對字面上的點?

用反斜線逸出。未逸出的點幾乎能比對任何字元,這是初學者最常犯的錯。

該用 regex 驗證電子郵件嗎?

只能寬鬆驗證。完整規格幾乎無法用樣式表達,建議檢查合理形狀後,再以實際寄信確認。

為什麼沒加 g 旗標也會列出所有符合項?

沒填 g 時工具會自動補上;旗標欄只保留 d、g、i、m、s、u、v、y 這幾個字母,其他字元會被默默丟棄。測試最多列出 1,000 個符合項;能符合空字串的樣式(例如 a*)每次會前進一個字元,因此不會無限迴圈。完全沒有符合時,清單顯示「沒有符合的項目。」,符合數顯示 0。

清單裡的 index 與 groups 代表什麼?

index 是符合項第一個字元在測試文字中從 0 起算的位置,每個換行算一個字元。groups 依序列出各擷取群組;沒有參與比對的群組顯示為 null,沒有括號的樣式則顯示空清單。

本站相關工具

以上步驟全部在這個頁面內完成,正規表示式測試器 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。