这是一个整洁的正则表达式,易于理解,维护和修改。

    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,

但是您如何使用这些?

text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,

有没有 美丽 有意义的某种形式并描述了它的功能?

有帮助吗?

解决方案

Regexbuddy 将为您“翻译”任何正直。给您的示例正则赠送时,它会输出:

((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)

Options: ^ and $ match at line breaks

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

这确实以文本形式看起来很吓人,但是以HTML形式(在这里无法复制)或RegexBuddy本身更具可读性。它还指出了常见的陷阱(例如重复捕获的群体,这可能不想要这里)。

其他提示

值得一提的是熟练以一行形式读取正则表达式。大多数情况下是这样写的

我喜欢 Expresso

过了一会儿,我已经习惯了阅读这些东西。大多数Regexes都没有太多,我建议该网站 http://www.regular-expressions.info/ 如果您想更频繁地使用它们。

正则表达只是表达口罩等的一种方式。最后,它只是一种具有自己的语法的“语言”。
评论您的正则表达式的每一点都与评论项目的每一行相同。
当然,这将帮助那些不了解您的代码的人,但是如果您(开发人员)了解正则含义,那是没有用的。

对我来说,阅读正则表达式与阅读代码是相同的。如果表达确实很复杂,则下面的解释可能很有用,但是在大多数情况下,没有必要。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top