Domanda

Questo è un pulito un'espressione regolare ben documentato, facile da comprendere, gestire e modificare.

    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,

Ma come si fa a lavorare con questi?

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

C'è una beautifier di qualche tipo che ha un senso di esso e descrive la sua funzionalità?

È stato utile?

Soluzione

RegexBuddy sarà "tradurre" qualsiasi regex per voi. Quando alimentate il vostro esempio regex, emette:

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

Questo ha un aspetto piuttosto intimidatorio in forma di testo, ma è molto più leggibile in forma HTML (che non può essere riprodotto qui) o in RegexBuddy sé. Si sottolinea inoltre grattacapi comuni (come la ripetizione gruppi di cattura che non è probabilmente voleva qui).

Altri suggerimenti

E 'vale la pena di diventare abili a leggere regexs sotto forma di una riga. Il più delle volte non ci sono scritte in questo modo

Mi piace espresso

Dopo un po ', io sono abituato a leggere le cose. Non c'è molto alla maggior parte regex, e vi consiglio il sito http://www.regular-expressions.info/ se si desidera utilizzare più spesso.

Le espressioni regolari sono solo un modo per esprimere le maschere, ecc Alla fine è solo un "linguaggio" con una propria sintassi.
Commento ogni momento della vostra espressione regolare sarebbe la stessa cosa di commentare ogni riga del vostro progetto.
Naturalmente sarebbe aiutare le persone che non capiscono il codice, ma è solo inutile se (lo sviluppatore) si capisce il significato della regex.

Per quanto mi riguarda, la lettura di espressioni regolari è la stessa cosa di lettura del codice. Se l'espressione è molto complesso una spiegazione di seguito potrebbe essere utile, ma il più delle volte non è necessario.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top