你如何理解一行写的正则表达式?

Rob*_*cks 6 regex maintainability

这是一个很好的文档正则表达式,易于理解,维护和修改.

    text = text.replace(/
    (                               // Wrap whole match in $1
        (
            ^[ \t]*>[ \t]?          // '>' at the start of a line
            .+\n                    // rest of the first line
            (.+\n)*                 // subsequent consecutive lines
            \n*                     // blanks
        )+
    )
    /gm,
Run Code Online (Sandbox Code Playgroud)

但是你如何处理这些?

text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,
Run Code Online (Sandbox Code Playgroud)

是否有某种美化器能够理解它并描述其功能?

enn*_*ler 5

值得努力擅长以一行形式阅读正则表达式.大部分时间都是用这种方式写的


Tim*_*ker 3

RegexBuddy将为您“翻译”任何正则表达式。当输入示例正则表达式时,它输出:

\n\n
((^[ \\t]*>[ \\t]?.+\\n(.+\\n)*\\n*)+)\n\nOptions: ^ and $ match at line breaks\n\nMatch the regular expression below and capture its match into backreference number 1 \xc2\xab((^[ \\t]*>[ \\t]?.+\\n(.+\\n)*\\n*)+)\xc2\xbb\n   Match the regular expression below and capture its match into backreference number 2 \xc2\xab(^[ \\t]*>[ \\t]?.+\\n(.+\\n)*\\n*)+\xc2\xbb\n      Between one and unlimited times, as many times as possible, giving back as needed (greedy) \xc2\xab+\xc2\xbb\n      Note: You repeated the capturing group itself.  The group will capture only the last iteration.  \n          Put a capturing group around the repeated group to capture all iterations. \xc2\xab+\xc2\xbb\n      Assert position at the beginning of a line (at beginning of the string or after a line break character) \xc2\xab^\xc2\xbb\n      Match a single character present in the list below \xc2\xab[ \\t]*\xc2\xbb\n         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) \xc2\xab*\xc2\xbb\n         The character \xe2\x80\x9c \xe2\x80\x9d \xc2\xab \xc2\xbb\n         A tab character \xc2\xab\\t\xc2\xbb\n      Match the character \xe2\x80\x9c>\xe2\x80\x9d literally \xc2\xab>\xc2\xbb\n      Match a single character present in the list below \xc2\xab[ \\t]?\xc2\xbb\n         Between zero and one times, as many times as possible, giving back as needed (greedy) \xc2\xab?\xc2\xbb\n         The character \xe2\x80\x9c \xe2\x80\x9d \xc2\xab \xc2\xbb\n         A tab character \xc2\xab\\t\xc2\xbb\n      Match any single character that is not a line break character \xc2\xab.+\xc2\xbb\n         Between one and unlimited times, as many times as possible, giving back as needed (greedy) \xc2\xab+\xc2\xbb\n      Match a line feed character \xc2\xab\\n\xc2\xbb\n      Match the regular expression below and capture its match into backreference number 3 \xc2\xab(.+\\n)*\xc2\xbb\n         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) \xc2\xab*\xc2\xbb\n         Note: You repeated the capturing group itself.  The group will capture only the last iteration.  \n             Put a capturing group around the repeated group to capture all iterations. \xc2\xab*\xc2\xbb\n         Match any single character that is not a line break character \xc2\xab.+\xc2\xbb\n            Between one and unlimited times, as many times as possible, giving back as needed (greedy) \xc2\xab+\xc2\xbb\n         Match a line feed character \xc2\xab\\n\xc2\xbb\n      Match a line feed character \xc2\xab\\n*\xc2\xbb\n         Between zero and unlimited times, as many times as possible, giving back as needed (greedy) \xc2\xab*\xc2\xbb\n
Run Code Online (Sandbox Code Playgroud)\n\n

这在文本形式中看起来确实相当吓人,但在 HTML 形式(无法在此处复制)或 RegexBuddy 本身中更具可读性。它还指出了常见的问题(例如重复捕获组,这可能是不需要的)。

\n