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

时间:2021-11-16 20:27:54

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?

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

5 个解决方案

#1


3  

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

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) «*»

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).

这在文本形式中看起来相当令人生畏,但它在HTML形式(这里不能再现)或RegexBuddy本身更具可读性。它还指出了常见的陷阱(例如重复捕获这里可能不需要的组)。

#2


5  

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

值得努力的是擅长以一行形式阅读正则表达式。大部分时间都是用这种方式写的

#3


2  

I like expresso

我喜欢expresso

#4


0  

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.

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

#5


0  

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.

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

#1


3  

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

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) «*»

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).

这在文本形式中看起来相当令人生畏,但它在HTML形式(这里不能再现)或RegexBuddy本身更具可读性。它还指出了常见的陷阱(例如重复捕获这里可能不需要的组)。

#2


5  

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

值得努力的是擅长以一行形式阅读正则表达式。大部分时间都是用这种方式写的

#3


2  

I like expresso

我喜欢expresso

#4


0  

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.

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

#5


0  

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.

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