Pregunta

Esta es una expresión regular bien documentado limpio, fácil de entender, mantener y modificar.

    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,

Pero, ¿cómo ir sobre el trabajo con estos?

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

¿Hay una embellecedor de algún tipo que da sentido y describe su funcionalidad?

¿Fue útil?

Solución

RegexBuddy va a "traducir" cualquier expresión regular para usted. Cuando se alimenta el ejemplo de expresiones regulares, que da salida:

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

Esto se parece bastante intimidante en forma de texto, pero es mucho más fácil de leer en el formulario HTML (que no puede ser reproducido aquí) o en la misma RegexBuddy. También señala errores comunes (tales como la repetición de grupos de captura que probablemente no se quiere aquí).

Otros consejos

Vale la pena el esfuerzo para convertirse en expertos en la lectura regexs en la forma de una línea. La mayoría de las veces no están escritas de esta manera

Después de un tiempo, me he acostumbrado a la lectura de las cosas. No hay mucho a la mayoría de expresiones regulares, y recomiendo el sitio http://www.regular-expressions.info/ si desea utilizarlos con más frecuencia.

Las expresiones regulares son sólo una forma de expresar máscaras, etc. Al final es sólo una "lengua", con su propia sintaxis.
Comentar cada bit de la expresión regular sería el mismo que el comentario de cada línea de su proyecto.
Por supuesto que sería ayudar a la gente que no entienda su código, pero sólo es inútil si (el promotor) no entiende el significado de la expresión regular.

Para mí, la lectura de las expresiones regulares es lo mismo que leer el código. Si la expresión es realmente compleja una explicación a continuación podría ser útil, pero la mayoría de las veces no es necesario.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top