views:

91

answers:

5

This is a neat well documented regular expression, easy to understand, maintain and modify.

    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,

But how do you go about working with these?

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

Is there a beautifier of some sort that makes sense of it and describes its functionality?

A: 

After a while, I've gotten used to reading the things. There is not much to most regexes, and I recommend the site http://www.regular-expressions.info/ if you want to use them more often.

Ryan Gooler
+5  A: 

It's worth the effort to become adept at reading regexs in the one line form. Most of the time there are written this way

ennuikiller
Yup just like any programming language syntax, after awhile it just becomes readable.
Chris
A: 

Regular expressions are just a way to express masks, etc. At the end it's just a "language" with its own syntax.
Comment every bit of your regular expression would be the same thing as comment every line of your project.
Of course it would help people who doesn't understand your code, but it's just useless if you (the developer) do understand the meaning of the regex.

For me, reading regular expressions is the same thing as reading code. If the expression is really complex an explanation below could be useful but most of the time it isn't necessary.

Colin Hebert
+3  A: 

RegexBuddy will "translate" any regex for you. When fed your example regex, it outputs:

((^[ \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) «*»

This does look rather intimidating in text form, but it's much more readable in HTML form (which can't be reproduced here) or in RegexBuddy itself. It also points out common gotchas (such as repeating capturing groups which is probably not wanted here).

Tim Pietzcker
Wow, it's pretty verbose. But could be helpful to learn regex.
Colin Hebert
Frankly speaking I find these heavy explanation not much easier understandable than the 1-line regex.
KennyTM
+1  A: 

I like expresso

Paul Creasey