6

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

    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,

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

4

5 回答 5

5

熟练地阅读单行形式的正则表达式是值得的。大部分时间都是这样写的

于 2010-08-29T20:23:56.827 回答
3

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 本身中更具可读性。它还指出了常见的问题(例如重复捕获组,这可能在这里不需要)。

于 2010-08-29T20:28:45.497 回答
2

我喜欢快递

于 2010-08-29T20:32:08.250 回答
0

一段时间后,我已经习惯了阅读这些东西。大多数正则表达式并不多,如果您想更频繁地使用它们,我推荐网站http://www.regular-expressions.info/ 。

于 2010-08-29T20:23:23.363 回答
0

正则表达式只是表达掩码等的一种方式。归根结底,它只是一种具有自己语法的“语言”。
注释正则表达式的每一点与注释项目的每一行都是一样的。
当然,它会帮助那些不理解您的代码的人,但如果您(开发人员)确实理解了正则表达式的含义,它就毫无用处。

对我来说,阅读正则表达式和阅读代码是一回事。如果表达式真的很复杂,下面的解释可能很有用,但大多数时候没有必要。

于 2010-08-29T20:25:58.717 回答