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

时间:2010-08-29 20:20:18

标签: 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,

但你怎么去处理这些?

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

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

5 个答案:

答案 0 :(得分:5)

值得努力学习以一行形式阅读正则表达式。大部分时间都是这样写的

答案 1 :(得分: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本身更具可读性。它还指出了常见的陷阱(例如重复捕获这里可能不需要的组)。

答案 2 :(得分:2)

我喜欢expresso

答案 3 :(得分:0)

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

答案 4 :(得分:0)

正则表达式只是表达蒙版等的一种方式。最后,它只是一种具有自己语法的“语言”。
评论正则表达式的每一位都与评论项目的每一行是一回事 当然,它会帮助那些不理解你的代码的人,但是如果你(开发人员)确实理解了正则表达式的含义,它就没用了。

对我来说,阅读正则表达式与阅读代码是一回事。如果表达式非常复杂,下面的解释可能很有用,但大部分时间都没有必要。