How do you understand regular expressions that are written on one line?

This is a neat, well-documented regular expression that is 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 are you going to work with them?

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

Is there a beautifier that makes sense and describes its functionality?

+6
regex maintainability
source share
5 answers

RegexBuddy will "translate" any regular expression for you. When you give an example of a regular expression, 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 looks pretty intimidating in text form, but much more readable in HTML form (which cannot be reproduced here) or in RegexBuddy itself. It also indicates common errors (for example, repeated capture groups, which are probably not needed here).

+3
source share

It is worth the effort to become adept at reading regular expressions on one line. Most of the time it is written there.

+5
source share

I like expresso

+2
source share

After some time, I got used to reading things. For most regular expressions there are not many, and I recommend the site http://www.regular-expressions.info/ if you want to use them more often.

0
source share

Regular expressions are just a way of expressing masks, etc. In the end, it's just a “language” with its own syntax.
Commenting on every bit of your regular expression will be the same as commenting on every line of your project.
Of course, this will help people who do not understand your code, but it is useless if you (the developer) understand the meaning of regular expression.

For me, reading regular expressions is the same as reading code. If the expression is really complex, the explanation below may be useful, but in most cases this is not necessary.

0
source share

All Articles