Frage

Dies ist ein sauberer gut dokumentierte regulärer Ausdruck, leicht zu verstehen, pflegen und ändern.

    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,

Aber wie gehen Sie mit diesen arbeiten?

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

Gibt es eine beautifier von einer Art, die Sinn für sie macht und beschreibt seine Funktionalität?

War es hilfreich?

Lösung

RegexBuddy wird "übersetzen" jede Regex für Sie. Wenn Ihr Beispiel regex zugeführt, gibt sie:

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

Das sieht eher in Textform einschüchternd, aber es ist viel besser lesbar in HTML-Form (die hier nicht wiedergegeben) oder in RegexBuddy selbst. Er weist auch darauf hin, häufig vorkommenden Problemfälle (wie die Erfassung Gruppen zu wiederholen, die wahrscheinlich wollte nicht hier).

Andere Tipps

Es ist die Mühe wert, geschickt zu werden, bei regexs in einer Zeile Form zu lesen. Die meiste Zeit gibt es auf diese Weise geschrieben

Ich mag Expresso

Nach einer Weile habe ich auf das Lesen der Dinge gewöhnt. Es gibt nicht viel zu den meisten Regexes, und ich empfehle die Seite http://www.regular-expressions.info/ , wenn Sie wollen, dass sie oft mehr verwenden.

Reguläre Ausdrücke sind nur eine Möglichkeit, Masken zum Ausdruck bringen, usw. Am Ende ist es nur eine „Sprache“ mit einer eigenen Syntax.
Kommentieren jedes Bit Ihrer regulären Ausdruck wäre das Gleiche wie Kommentar jede Zeile Ihres Projekts.
Natürlich wäre es Menschen helfen, die Ihren Code nicht verstehen, aber es ist nur nutzlos, wenn Sie (der Entwickler) die Bedeutung der Regex verstehen.

Für mich, reguläre Ausdrücke zu lesen ist die gleiche wie Code zu lesen. Wenn der Ausdruck wirklich komplex ist unten eine Erklärung nützlich sein könnte, aber die meiste Zeit ist es nicht erforderlich.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top