This pattern has 2 capturing groups with 2 flags (gi) and is broken down into 14 parts below.
Flags
gglobal - find all matches in the input, not just the first oneiignore case - letters match regardless of upper or lower case^Start-of-string anchor (or start of a line with the m flag)(?<user>Start of a named capturing group "user" - saves its match under that name[a-z0-9._%+-]A single character from the set: the range "a" to "z", the range "0" to "9", the character ".", the character "_", the character "%", the character "+", the character "-"+Repeated one or more times)End of the group@A literal "@" character(?<domain>Start of a named capturing group "domain" - saves its match under that name[a-z0-9.-]A single character from the set: the range "a" to "z", the range "0" to "9", the character ".", the character "-"+Repeated one or more times\.A literal "." character (the backslash removes its special meaning)[a-z]A single character from the set: the range "a" to "z"{2,}Repeated 2 or more times)End of the group$End-of-string anchor (or end of a line with the m flag)
正規表示式解釋器採用正規表示式並將其翻譯成簡單的英語,一次一個結構。 而不是像 ^(?<user>[a-z0-9._%+-]+)@... 你會得到一個標記的細分:這部分是錨點,這是一個命名捕捉組,這個字元類別的意思是「小寫字母、數字、點或連字號」,這個量詞的意思是「一次或多次」。 它將一面符號牆變成您可以閱讀的清單。
正規表達式設計緊湊,這使得它們強大但難以審查。 相同的模式可以以多種方式編寫,單一雜散反斜線或貪婪的量詞完全改變了意義。 閱讀不熟悉的正規表達式 - 在程式碼審查、舊設定檔或堆疊溢出答案中 - 通常意味著在心理上模擬引擎。 此工具為您進行模擬,並將結果顯示為一個有序的縮排列表,它反映了模式的結構,包括嵌套組和環視。
一切都在您的程式碼使用的同一個 JavaScript Regexp 引擎上運行客戶端,因此您在此處看到的標誌行為和有效性與生產相符。 您的模式從未上傳,沒有記錄任何內容,並且一旦頁面載入,該工具將繼續離線工作。 當您要理解的正規表達式來自私有程式碼庫或匹配敏感資料格式時,這很重要。
解釋器告訴您模式的內容,而不是它是否適合您的資料 - 要檢查它,請根據正規表示式測試器中的真實輸入運行它。 因為它是使用瀏覽器自己的引擎編譯,所以存在於 PCRE 中但不存在於 ECMAScript 中的結構以兩種不同的方式表現。 所有權量詞,如 A++、原子組 (?>...) 和遞歸 (?r) 被直接拒絕,帶有引擎自己的訊息。 更微妙的情況是 \a 和 \z:沒有 U 標誌 JavaScript 將它們視為身份轉義,因此從 PHP 或 Python 複製的模式被解釋為匹配文字 A 或 Z 而不是字串邊界,並且不會引發錯誤。 通常也描述了 (A+)+ 等嵌套量詞;災難性回溯是輸入的運行時屬性,而不是結構中可見的東西。
輸入沒有周圍斜線的模式 - 只是表達本身。 按一下載入範例以從工作的電子郵件匹配範例開始。
切換模式使用的標誌(G、I、M、S、U、Y、D)。 解釋器報告每個標誌的作用,並驗證針對該標誌組合的模式。
每個令牌都按順序列出,並帶有簡單的英語描述。 嵌套組是縮進的,因此圖案的結構一目了然。
複製完整的純文字分解以貼上到程式碼註解、拉取請求說明或文件中,這樣下一個閱讀器就不必解碼。
每個錨點、字元類別、量詞、群組和 escape 都有自己的線條,並按照引擎讀取它們的順序
捕獲、非捕獲、命名組和所有四種環顧類型都被標記和縮排,因此嵌套結構很容易遵循
G、I、M、S、U、Y 和 D 標誌都在上下文中描述,因此您了解它們如何改變整個匹配,而不僅僅是語法
模式是使用瀏覽器原生 Regexp 引擎驗證的,因此在您依賴它之前,它會發現一個無效的表達式有確切的錯誤
Extract email addresses, URLs, phone numbers, and IPv4 addresses from any block of text. De-duplicate, sort, and copy the results. Runs client-side.
Find and replace text with a regular expression, using capture group backreferences like $1 and named groups, with full flag support
Test regular expressions against sample text with live match highlighting, capture groups, and full flag support
Build, test, and debug regular expressions with real-time matching and a comprehensive cheatsheet
0 comments
No comments yet. Be the first to share your thoughts!